Entity Framework Core is a lightweight, extensible, open source and cross-platform version of the popular Entity Framework data access technology.
It can serve as an object-relational mapper (ORM), enabling .NET developers to work with a database using .NET objects.
It eliminates the need for most of the data-access code that developers usually need to write.
It supports LINQ queries, change tracking, updates, and schema migrations.
It can be database-first, model-first or code-first.
Steps to use Entity Framework Core in an ASP.NET Core application
First, create a new project in Visual Studio with the ASP.NET Core Web Application (Model-View-Controller) template.
Then, install the Entity Framework Core package using the NuGet Package Manager.
Right-click on the project in Solution Explorer and select Manage NuGet Packages.
Search for Microsoft.EntityFrameworkCore and install the package.
Microsoft.EntityFrameworkCore.SqlServer -> for SQL Server database provider.
Microsoft.EntityFrameworkCore.Tools -> for EF Core tools such as migrations.
Create a model class that represents a table in the database. Let's say we have a Officer table.
publicclassOfficer {publicGuid Id { get; set; }publicstring Name { get; set; }publicstring Gender { get; set; }publicstring Phone { get; set; }publicstring Department { get; set; }publicstring Position { get; set; } }
Create a DbContext class that represents a session with the database. It is used to query and save instances of the model classes.
namespaceEntity_Framework_CRUD.Models{publicclassOfficerContext:DbContext {publicOfficerContext(DbContextOptions<OfficerContext> options) : base(options) { }publicDbSet<Officer> tbl_officer { get; set; } // This is the table name i.e tbl_officer, // We can name it anything we want, but it should be same as the table name in the database. }}
For the Read operation, i.e Read a list, create Index action method to display the list of officers, which will be shown at the root URL and return the view.
Then, we need to create a post method to save the data to the database.
Controller/OfficerController.cs => Post Method
[HttpPost]publicIActionResultCreateOfficer(Officer o){var officer =newOfficer(){ Id =Guid.NewGuid(), Name =o.Name, Gender =o.Gender, Department =o.Department, Phone =o.Phone, Position =o.Position };_context.tbl_officer.Add(officer);_context.SaveChanges();returnRedirectToAction("Index");}
This will save the data to the database and redirect to the Index page.
Read Operation (Single Record)
For the Read operation, i.e Read a single record, create GetOfficer action method to display the details of a single officer. We need to pass the id of the officer to get the details.
This will update the data in the database and redirect to the Index page.
Delete Operation
For the Delete operation, create DeleteOfficer action method to delete an officer. We need to pass the id of the officer to delete. We can just create a post method for this operation as we are not displaying any form.