CRUD Operations with Entity Framework Core

IHUB Talent – The Best Full Stack .NET Training Institute in Hyderabad with Live Internship Program

If you're planning to build a rewarding career in software development, then IHUB is the perfect place to begin. Recognized as the best Full Stack .NET training institute in Hyderabad, IHUB offers a complete job-oriented course with a live intensive internship program, guided by real-time industry experts.

Whether you're a graduate, postgraduate, someone with an educational gap, or planning a job domain change, IHUB’s Full Stack .NET course is designed to make you job-ready in just a few months. The program is structured for freshers and working professionals alike, offering hands-on experience and placement support.

CRUD Operations with Entity Framework Core

Entity Framework Core (EF Core) is a modern, lightweight, and cross-platform ORM (Object-Relational Mapper) developed by Microsoft. It enables developers to work with a database using .NET objects, eliminating the need for most of the data-access code traditionally written. One of the most common tasks in application development is performing CRUD operations — Create, Read, Update, and Delete — and EF Core simplifies this significantly.

1. Create (Insert Data)

To insert a new record using EF Core, you first create an instance of the model class and add it to the database context:

csharp

var product = new Product { Name = "Laptop", Price = 45000 };

context.Products.Add(product);

context.SaveChanges();

The Add() method queues the object for insertion, and SaveChanges() commits it to the database.

2. Read (Retrieve Data)

Reading data is simple with LINQ queries:

csharp

var products = context.Products.ToList(); // Get all

var product = context.Products.FirstOrDefault(p => p.Id == 1); // Get by Id

You can also use advanced LINQ features for filtering, sorting, and joining.

3. Update (Modify Data)

To update a record, retrieve it from the database, modify its properties, and call SaveChanges():

csharp

var product = context.Products.First(p => p.Id == 1);

product.Price = 50000;

context.SaveChanges();

EF Core tracks changes automatically and updates only the modified fields.

4. Delete (Remove Data)

To delete a record, retrieve it and remove it using the Remove() method:

csharp

var product = context.Products.First(p => p.Id == 1);

context.Products.Remove(product);

context.SaveChanges();

This will delete the corresponding row from the database.

Conclusion

EF Core makes CRUD operations intuitive and efficient by leveraging LINQ and change tracking. It supports multiple database providers, including SQL Server, SQLite, PostgreSQL, and MySQL. Whether you're building a web application with ASP.NET Core or a desktop app, EF Core helps you build scalable and maintainable data access layers with minimal effort.

Read More

RESTful API Development using ASP.NET Core Web API

Frontend Validation in ASP.NET

Using Angular with .NET Core

Visit Our I-HUB Talent Training Institute in Hyderabad

Comments

Popular posts from this blog

Why Choose .NET for Full Stack Development?

What is Full Stack .NET Development?

What is ASP.NET Core?