3.what Is The DbContext Class in EF Core
3.what Is The DbContext Class in EF Core
The DbContext class in Entity Framework Core is a crucial component that represents a session with
the database, allowing you to query and save instances of your entities. It acts as a bridge between
your domain or entity classes and the database, providing mechanisms to perform CRUD (Create,
Read, Update, Delete) operation.
Change Tracking: When retrieving data from the database using the
DbContext, the changes made to that data are tracked by the
DbContext. If any modifications, the entity’s properties are updated,
DbContext keeps track of those changes. This enables EF Core to
generate appropriate SQL statements to reflect the database changes
accurately.
1. To use the DbContext class ,we must create a class derived from
the DbContext class.
DbSet Properties: Inside your DbContext class, add DbSet<TEntity> properties for each entity type
you want to include in your model. These properties will represent tables in your database.
EXAMPLE:
public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; }
Example
Have a look ?
The constructor public EFCoreDbContext() : base() is used to initialize an instance of the
EFCoreDbContext class by calling the constructor of the base class DbContext. This constructor is
typically used when you want to use the default constructor of the base class, which does not take
any parameters.
In Entity Framework Core, the DbContext class has several constructors, some of which accept
parameters such as DbContextOptions. When you use a constructor that accepts parameters, you
need to explicitly call the base class constructor with those parameters. However, if you want to use
the default constructor of the base class, you can simply call base() in your derived class constructor.
2. Database: Provides access to database-related information and operations for this context.
3. Model: Returns the metadata about the shape of entities, the relationships between them,
and how they map to the database.
4. Options: Provides access to the options that were used to create the context.
6. Set<TEntity>(): Returns a DbSet<TEntity> instance for access to entities of the given type in
the context.
8. Entry(entity): Provides access to information and operations for a given entity being tracked
by the context.
9. Find<TEntity>(keyValues): Finds an entity with the given primary key values. If an entity with
the given primary key values is being tracked by the context, it is returned immediately
without making a request to the database. Otherwise, a query is made to the database for
an entity with the given primary key values, and it is returned if found.
10. Local: Gets a collection of all the entities that are currently being tracked by the context.
11. ChangeTracker.Entries(): Gets a collection of all the entities that are currently being tracked
by the context.
7. Entry: Gets an EntityEntry for the given entity. The entry provides access to
change tracking information and operations for the entity. So, using this
method, we can manually set the Entity State, which DbContext will also
track.
10.Remove: It sets the Deleted state to the specified entity, which will delete
the data when SaveChanges() is called.
11.RemoveRange: Sets Deleted state to a collection of entities that will delete
the data in a single DB round trip when SaveChanges() is called.
14.Set: Creates a DbSet<TEntity> that can be used to query and save instances
of TEntity.