DbContext Class
Namespace: Microsoft.EntityFrameworkCore
Assembly: Microsoft.EntityFrameworkCore.dll
Summary
Represents a session around a database. This class is the entry point to query and save data in a Code First enabled object context.
DbContext is a context which monitors change tracking and identity resolution for objects that are loaded into the context. The context is often referred to as the "work unit" pattern and a "repository".
Syntax
C#
public abstract class DbContext : IDisposable
Inheritance
System.ObjectMicrosoft.EntityFrameworkCore.DbContext
Remarks
DbContext provides a central point for interacting with your database. It manages the lifecycle of entities, tracks their changes, and facilitates saving those changes back to the database.
Key responsibilities include:
- Change Tracking: Monitors entities loaded into the context for modifications, additions, and deletions.
- Identity Resolution: Ensures that each entity instance is represented by a single object in the context.
- Querying: Provides access to
DbSet<TEntity>properties, which are used to query entities from the database. - Saving Changes: Handles the process of persisting changes to the database using methods like
SaveChanges()andSaveChangesAsync(). - Concurrency Control: Manages optimistic concurrency using row versioning or timestamps.
You typically create a class that derives from DbContext and defines DbSet<TEntity> properties for each entity in your domain model.
For detailed usage and configuration, please refer to the Entity Framework Core documentation.
Constructors
DbContext()
Initializes a new instance of the DbContext class. This constructor is typically called by derived context classes when they are created by the framework.
protected DbContext();
DbContext(DbContextOptions options)
Initializes a new instance of the DbContext class with the given options. This constructor is typically called by derived context classes when they are created by the framework.
protected DbContext(Microsoft.EntityFrameworkCore.DbContextOptions options);
Methods
SaveChanges()
Saves all changes made in this context to the underlying database.
public virtual int SaveChanges();
Returns
The number of state entries written to the underlying database. This can include state entries for objects inserted, updated, or deleted.
SaveChangesAsync(CancellationToken cancellationToken = default)
Asynchronously saves all changes made in this context to the underlying database.
public virtual Task<int> SaveSaveChangesAsync(System.Threading.CancellationToken cancellationToken = default);
Returns
A task that represents the asynchronous operation. The task result contains the number of state entries written to the underlying database. This can include state entries for objects inserted, updated, or deleted.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose();
Set<TEntity>()
Gets an instance of DbSet<TEntity> which can be used to query and save instances of TEntity.
public virtual Microsoft.EntityFrameworkCore.DbSet<TEntity> Set<TEntity>() where TEntity : class;
Type Parameters
TEntity: The type of objects that are the target of operations performed by this DbSet.
Returns
A DbSet<TEntity> that can be used to query and save instances of TEntity.
Properties
ChangeTracker
Gets an instance of ChangeTracker which is used to track the state of entities in the context.
public virtual Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker ChangeTracker { get; }
Database
Gets an instance of DatabaseFacade which provides access to database-related operations for this context.
public virtual Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade Database { get; }
Model
Gets the IModel for this context.
public virtual Microsoft.EntityFrameworkCore.Metadata.IModel Model { get; }
Implements
System.IDisposable