Asp.Net Core: DbContext In EntityFramework core

Introduction :

The DbContext class is One of the very important class of EntityFramework. This is the class that we use in our application code to interact with the underlying database. DbContext In EntityFramework core.

DbContext is a combination of the Unit Of Work and Repository patterns, DbContext class that manages the database connection and is used to retrieve and save data in the database, Asp.Net Core: DbContext In entityframework core.

To use DbContext in our application, we need to create the class that derives from DbContext, also known as context class. This context class typically includes DbSet<TEntity> properties for each entity in the model.

DbContext class is available in namespace: Microsoft.EntityFrameworkCore.

DbContext In EntityFramework core:

Consider the following example of context class in EF Core


    public class AppDbContext :DbContext
    {

    }

  • DbContext class can be able to do any useful work, it needs an instance of the DbContextOptions class.
  • The DbContextOptions instance carries configuration information such as the connection string, database provider to use etc.
  • The DbContextOptions instance carries configuration information such as the connection string, database provider to use etc.

    public class AppDbContext :DbContext
    {
        public AppDbContext(DbContextOptions options ) :base (options)
        {

        }
        public DbSet<EmployeeModel> Employees { get; set; }
    }

Entity Framework Core DbSet

The DbContext class includes a DbSet<TEntity> property for each entity in the model.in our application we have, only one entity class:EmployeeModel.

In our application we have class one DbSet<EmployeeModel> property. So here will use this DbSet property Employees to query and save instances of the EmployeeModel class.

Also Learn More Tutorial :

 

1 Comment

Leave a Reply

Your email address will not be published.