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 :
- ASP.Net Core: Form Submit (Post) with Example
- File(Image) Upload in ASP.NET Core MVC with Example
- .Net Core: Read Connection String from AppSettings.json file
- CRUD Operation Using Asp.Net Core Mvc with Repository Pattern
- Display Data in GridView(Grid) Using ASP.Net Core MVC
- Asp.Net Core: DbContext In EntityFramework core
- Difference between AddSingleton vs AddScoped vs AddTransient in asp.net core
- Difference between .NET Core and .NET Framework
- Difference between TempData keep() And Peek() in Asp.Net MVC
- Asp.Net Image Upload in 3-Tier Architecture and store in sql database
- ViewImports in ASP.NET Core MVC
1 Comment