What is DBContext and DBSet

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.

This post was translated from Spanish. You can see the original one here.
If there is any problem you can add a comment bellow or contact me in the website's contact form

Uso del bloqueador de anuncios adblock

Hola!

Primero de todo bienvenido a la web de NetMentor donde podrás aprender programación en C# y .NET desde un nivel de principiante hasta más avanzado.


Yo entiendo que utilices un bloqueador de anuncios como AdBlock, Ublock o el propio navegador Brave. Pero te tengo que pedir por favor que desactives el bloqueador para esta web.


Intento personalmente no poner mucha publicidad, la justa para pagar el servidor y por supuesto que no sea intrusiva; Si pese a ello piensas que es intrusiva siempre me puedes escribir por privado o por Twitter a @NetMentorTW.


Si ya lo has desactivado, por favor recarga la página.


Un saludo y muchas gracias por tu colaboración

© copyright 2025 NetMentor | Todos los derechos reservados | RSS Feed

Buy me a coffee Invitame a un café