In this video we will discuss the significance of the DbContext class in Entity Framework Core
Text version of the video
[ Ссылка ]
Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.
[ Ссылка ]
Slides
[ Ссылка ]
ASP.NET Core Text Articles & Slides
[ Ссылка ]
ASP.NET Core Tutorial
[ Ссылка ]
Angular, JavaScript, jQuery, Dot Net & SQL Playlists
[ Ссылка ]
One of the very important classes in Entity Framework Core is the DbContext class. This is the class that we use in our application code to interact with the underlying database. It is this class that manages the database connection and is used to retrieve and save data in the database.
To use the DbContext class in our application
We create a class that derives from the DbContext class.
DbContext class is in Microsoft.EntityFrameworkCore namespace.
public class AppDbContext : DbContext
{ }
For the DbContext class to 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.
To pass the DbContextOptions instance we use the constructor as shown in the example below.
We will discuss more about the DbContextOptions class in our next video when we discuss database connection string in ASP.NET Core.
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions[AppDbContext] options)
: base(options)
{
}
}
The DbContext class includes a DbSet[TEntity] property for each entity in the model.
At the moment in our application we have, only one entity class - Employee.
So in our AppDbContext class we only have one DbSet[Employee] property.
We will use this DbSet property Employees to query and save instances of the Employee class.
The LINQ queries against the DbSet[TEntity] will be translated into queries against the underlying database.
We will see this in action in our upcoming videos.
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions[AppDbContext] options)
: base(options)
{
}
public DbSet[Employee] Employees { get; set; }
}
To be able to connect to a database we need the database connection string. In our next video, we will discuss, where do define the connection string and using it in Entity Framework Core.
DbContext in entity framework core
Теги
ef core dbcontextdbcontext example in ef coreef core dbset propertiesef core dbcontextoptions exampleentity framework core add dbcontextentity framework core dbcontext best practicesentity framework core dbcontext constructorentity framework core dbcontext exampleentity framework core code first dbcontext