Entity Framework Core

  • 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

  1. First, create a new project in Visual Studio with the ASP.NET Core Web Application (Model-View-Controller) template.

  2. 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.

  3. Create a model class that represents a table in the database. Let's say we have a Officer table.

     public class Officer
     {
         public Guid Id { get; set; }
         public string Name { get; set; }
         public string Gender { get; set; }
         public string Phone { get; set; }
         public string Department { get; set; }
         public string Position { get; set; }
     }
  4. Create a DbContext class that represents a session with the database. It is used to query and save instances of the model classes.

    namespace Entity_Framework_CRUD.Models
    {
        public class OfficerContext : DbContext
        {
            public OfficerContext(DbContextOptions<OfficerContext> options) : base(options) { }
    
            public DbSet<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.
        }
    }
    
  5. Register the DbContext in the Program.cs file.

    builder.Services.AddDbContext<OfficerContext>(options => {
       options.UseSqlServer(builder.Configuration.GetConnectionString("connectionString"));
    });
  6. In appsettings.json file, add the connection string for the database.

  1. Create a migration to generate the database schema.

    • Open the Package Manager Console.

    • Run the command Add-Migration "Initial" to create a migration.

    • Run the command Update-Database to apply the migration and create the database.

  2. Use the DbContext in a controller to query and save data.

CRUD Operations

  • Create a new Controller for the model, i.e. OfficerController.

  • Then, add the DbContext in the Controller using Dependency Injection.

Read Operation (List)

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.

  • Controller/OfficerController.cs

  • Views/Officer/Index.cshtml

Create Operation

For the Create operation, create CreateOfficer action method to display the form to create a new officer. It must have a get and post method.

  • Controller/OfficerController.cs => Get Method

This will return the view CreateOfficer.cshtml which will have a form to create a new officer.

  • Views/Officer/CreateOfficer.cshtml

Then, we need to create a post method to save the data to the database.

  • Controller/OfficerController.cs => Post Method

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.

  • Controller/OfficerController.cs => Get Method

This will return the view GetOfficer.cshtml which will display the details of the officer.

  • Views/Officer/GetOfficer.cshtml

Update Operation

For the Update operation, create UpdateOfficer action method to display the form to update an officer. It must have a get and post method.

  • Controller/OfficerController.cs => Get Method

This will return the view UpdateOfficer.cshtml which will have a form to update an officer.

  • Views/Officer/UpdateOfficer.cshtml

Then, we need to create a post method to update the data in the database.

  • Controller/OfficerController.cs => Post Method

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.

  • Controller/OfficerController.cs => Post Method

Last updated

Was this helpful?