Today we are going to look at two of the most important classes when working with Entity Framework, whether it's core or the standard version: DbContext
and DbSet
.
1 - What is DBContext?
DbContext
is a class in Entity Framework Core that is used to interact with a database. It is the primary working class used to perform CRUD operations (Create, Read, Update, and Delete) on the database.
DbContext is an abstract class that you can inherit to create a concrete class representing your application’s database context. This concrete class is used to configure the database connection, define the entities that will be stored in the database, and apply changes to the database.
Here is a code example illustrating how to create a CursoEfContext
class that inherits from DbContext
:
public class CursoEfContext : DbContext
{
public CursoEfContext()
{
}
}
In this example, we have created a CursoEfContext
class that inherits from DbContext
.
2 - What is DbSet?
DbSet
is a class in Entity Framework Core that represents a collection of entities in the database. For example, DbSet<Userid>
or DbSet<Working experience>
.
Each DbSet
property in a DbContext
class represents a table in the database. For example, in the following code sample, we have our dbcontext in the CursoEfContext
class that defines two DbSet
properties: one for Userid
entities and one for WorkingExprience
entities:
public class CursoEfContext : DbContext
{
public DbSet<Userid> Userids { get; set; }
public DbSet<Wokringexperience> Wokringexperiences { get; set; }
}
Once we've defined the DbSet
, we can use it to perform CRUD operations. (which we'll see later)
Conclusion
DbContext
is an essential class in Entity Framework Core that provides an abstraction layer over the database and allows you to interact with it easily and efficiently in your application.
DbSet
is a class in Entity Framework Core that is used to define a collection of database entities and perform CRUD operations on them within the application.
If there is any problem you can add a comment bellow or contact me in the website's contact form