DbContext Class

Definition

A DbContext instance represents a session with the database and can be used to query and save instances of your entities. DbContext is a combination of the Unit Of Work and Repository patterns.

public class DbContext : IDisposable, Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<IServiceProvider>, Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies, Microsoft.EntityFrameworkCore.Internal.IDbContextPoolable, Microsoft.EntityFrameworkCore.Internal.IDbSetCache
Inheritance
DbContext
Implements

Remarks

Typically you create a class that derives from DbContext and contains DbSet<TEntity> properties for each entity in the model. If the DbSet<TEntity> properties have a public setter, they are automatically initialized when the instance of the derived context is created.

Override the OnConfiguring(DbContextOptionsBuilder) method to configure the database (and other options) to be used for the context. Alternatively, if you would rather perform configuration externally instead of inline in your context, you can use DbContextOptionsBuilder<TContext> (or DbContextOptionsBuilder) to externally create an instance of DbContextOptions<TContext> (or DbContextOptions) and pass it to a base constructor of DbContext.

The model is discovered by running a set of conventions over the entity classes found in the DbSet<TEntity> properties on the derived context. To further configure the model that is discovered by convention, you can override the OnModelCreating(ModelBuilder) method.

Constructors

DbContext()

Initializes a new instance of the DbContext class. The OnConfiguring(DbContextOptionsBuilder) method will be called to configure the database (and other options) to be used for this context.

DbContext(DbContextOptions)

Initializes a new instance of the DbContext class using the specified options. The OnConfiguring(DbContextOptionsBuilder) method will still be called to allow further configuration of the options.

Properties

ChangeTracker

Provides access to information and operations for entity instances this context is tracking.

Database

Provides access to database related information and operations for this context.

Model

The metadata about the shape of entities, the relationships between them, and how they map to the database.

Methods

Add(Object)

Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

Add<TEntity>(TEntity)

Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

AddAsync(Object, CancellationToken)

Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

This method is async only to allow special value generators, such as the one used by 'Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo', to access the database asynchronously. For all other cases the non async method should be used.

AddAsync<TEntity>(TEntity, CancellationToken)

Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

This method is async only to allow special value generators, such as the one used by 'Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo', to access the database asynchronously. For all other cases the non async method should be used.

AddRange(IEnumerable<Object>)

Begins tracking the given entities, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

AddRange(Object[])

Begins tracking the given entities, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

AddRangeAsync(IEnumerable<Object>, CancellationToken)

Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

This method is async only to allow special value generators, such as the one used by 'Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo', to access the database asynchronously. For all other cases the non async method should be used.

AddRangeAsync(Object[])

Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

This method is async only to allow special value generators, such as the one used by 'Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo', to access the database asynchronously. For all other cases the non async method should be used.

Attach(Object)

Begins tracking the given entity in the Unchanged state such that no operation will be performed when SaveChanges() is called.

A recursive search of the navigation properties will be performed to find reachable entities that are not already being tracked by the context. These entities will also begin to be tracked by the context. If a reachable entity has its primary key value set then it will be tracked in the Unchanged state. If the primary key value is not set then it will be tracked in the Added state. An entity is considered to have its primary key value set if the primary key property is set to anything other than the CLR default for the property type.

Attach<TEntity>(TEntity)

Begins tracking the given entity in the Unchanged state such that no operation will be performed when SaveChanges() is called.

A recursive search of the navigation properties will be performed to find reachable entities that are not already being tracked by the context. These entities will also begin to be tracked by the context. If a reachable entity has its primary key value set then it will be tracked in the Unchanged state. If the primary key value is not set then it will be tracked in the Added state. An entity is considered to have its primary key value set if the primary key property is set to anything other than the CLR default for the property type.

AttachRange(IEnumerable<Object>)

Begins tracking the given entities in the Unchanged state such that no operation will be performed when SaveChanges() is called.

A recursive search of the navigation properties will be performed to find reachable entities that are not already being tracked by the context. These entities will also begin to be tracked by the context. If a reachable entity has its primary key value set then it will be tracked in the Unchanged state. If the primary key value is not set then it will be tracked in the Added state. An entity is considered to have its primary key value set if the primary key property is set to anything other than the CLR default for the property type.

AttachRange(Object[])

Begins tracking the given entities in the Unchanged state such that no operation will be performed when SaveChanges() is called.

A recursive search of the navigation properties will be performed to find reachable entities that are not already being tracked by the context. These entities will also begin to be tracked by the context. If a reachable entity has its primary key value set then it will be tracked in the Unchanged state. If the primary key value is not set then it will be tracked in the Added state. An entity is considered to have its primary key value set if the primary key property is set to anything other than the CLR default for the property type.

Dispose()

Releases the allocated resources for this context.

Entry(Object)

Gets an EntityEntry for the given entity. The entry provides access to change tracking information and operations for the entity.

This method may be called on an entity that is not tracked. You can then set the State property on the returned entry to have the context begin tracking the entity in the specified state.

Entry<TEntity>(TEntity)

Gets an EntityEntry<TEntity> for the given entity. The entry provides access to change tracking information and operations for the entity.

Find(Type, Object[])

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then 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 this entity, if found, is attached to the context and returned. If no entity is found, then null is returned.

Find<TEntity>(Object[])

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then 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 this entity, if found, is attached to the context and returned. If no entity is found, then null is returned.

FindAsync(Type, Object[])

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then 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 this entity, if found, is attached to the context and returned. If no entity is found, then null is returned.

FindAsync(Type, Object[], CancellationToken)

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then 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 this entity, if found, is attached to the context and returned. If no entity is found, then null is returned.

FindAsync<TEntity>(Object[])

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then 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 this entity, if found, is attached to the context and returned. If no entity is found, then null is returned.

FindAsync<TEntity>(Object[], CancellationToken)

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then 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 this entity, if found, is attached to the context and returned. If no entity is found, then null is returned.

OnConfiguring(DbContextOptionsBuilder)

Override this method to configure the database (and other options) to be used for this context. This method is called for each instance of the context that is created.

In situations where an instance of DbContextOptions may or may not have been passed to the constructor, you can use IsConfigured to determine if the options have already been set, and skip some or all of the logic in OnConfiguring(DbContextOptionsBuilder).

OnModelCreating(ModelBuilder)

Override this method to further configure the model that was discovered by convention from the entity types exposed in DbSet<TEntity> properties on your derived context. The resulting model may be cached and re-used for subsequent instances of your derived context.

Remove(Object)

Begins tracking the given entity in the Deleted state such that it will be removed from the database when SaveChanges() is called.

Remove<TEntity>(TEntity)

Begins tracking the given entity in the Deleted state such that it will be removed from the database when SaveChanges() is called.

RemoveRange(Object[])

Begins tracking the given entity in the Deleted state such that it will be removed from the database when SaveChanges() is called.

RemoveRange(IEnumerable<Object>)

Begins tracking the given entity in the Deleted state such that it will be removed from the database when SaveChanges() is called.

SaveChanges(Boolean)

Saves all changes made in this context to the database.

SaveChanges()

Saves all changes made in this context to the database.

SaveChangesAsync(Boolean, CancellationToken)

Asynchronously saves all changes made in this context to the database.

SaveChangesAsync(CancellationToken)

Asynchronously saves all changes made in this context to the database.

Set<TEntity>()

Creates a DbSet<TEntity> that can be used to query and save instances of TEntity.

Update(Object)

Begins tracking the given entity in the Modified state such that it will be updated in the database when SaveChanges() is called.

All properties of the entity will be marked as modified. To mark only some properties as modified, use Attach(Object) to begin tracking the entity in the Unchanged state and then use the returned EntityEntry to mark the desired properties as modified.

A recursive search of the navigation properties will be performed to find reachable entities that are not already being tracked by the context. These entities will also begin to be tracked by the context. If a reachable entity has its primary key value set then it will be tracked in the Modified state. If the primary key value is not set then it will be tracked in the Added state. An entity is considered to have its primary key value set if the primary key property is set to anything other than the CLR default for the property type.

Update<TEntity>(TEntity)

Begins tracking the given entity in the Modified state such that it will be updated in the database when SaveChanges() is called.

All properties of the entity will be marked as modified. To mark only some properties as modified, use Attach<TEntity>(TEntity) to begin tracking the entity in the Unchanged state and then use the returned EntityEntry to mark the desired properties as modified.

A recursive search of the navigation properties will be performed to find reachable entities that are not already being tracked by the context. These entities will also begin to be tracked by the context. If a reachable entity has its primary key value set then it will be tracked in the Modified state. If the primary key value is not set then it will be tracked in the Added state. An entity is considered to have its primary key value set if the primary key property is set to anything other than the CLR default for the property type.

UpdateRange(Object[])

Begins tracking the given entities in the Modified state such that they will be updated in the database when SaveChanges() is called.

All properties of each entity will be marked as modified. To mark only some properties as modified, use Attach(Object) to begin tracking each entity in the Unchanged state and then use the returned EntityEntry to mark the desired properties as modified.

A recursive search of the navigation properties will be performed to find reachable entities that are not already being tracked by the context. These entities will also begin to be tracked by the context. If a reachable entity has its primary key value set then it will be tracked in the Modified state. If the primary key value is not set then it will be tracked in the Added state. An entity is considered to have its primary key value set if the primary key property is set to anything other than the CLR default for the property type.

UpdateRange(IEnumerable<Object>)

Begins tracking the given entities in the Modified state such that they will be updated in the database when SaveChanges() is called.

All properties of each entity will be marked as modified. To mark only some properties as modified, use Attach(Object) to begin tracking each entity in the Unchanged state and then use the returned EntityEntry to mark the desired properties as modified.

A recursive search of the navigation properties will be performed to find reachable entities that are not already being tracked by the context. These entities will also begin to be tracked by the context. If a reachable entity has its primary key value set then it will be tracked in the Modified state. If the primary key value is not set then it will be tracked in the Added state. An entity is considered to have its primary key value set if the primary key property is set to anything other than the CLR default for the property type.

Explicit Interface Implementations

IInfrastructure<IServiceProvider>.Instance

Gets the scoped IServiceProvider being used to resolve services.

This property is intended for use by extension methods that need to make use of services not directly exposed in the public API surface.

IDbContextDependencies.ChangeDetector

This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

IDbContextDependencies.EntityFinderSource

This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

IDbContextDependencies.EntityGraphAttacher

This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

IDbContextDependencies.InfrastructureLogger

This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

IDbContextDependencies.QueryProvider

This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

IDbContextDependencies.SetSource

This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

IDbContextDependencies.StateManager

This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

IDbContextDependencies.UpdateLogger

This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

IDbContextPoolable.ResetState()
IDbContextPoolable.Resurrect(DbContextPoolConfigurationSnapshot)
IDbContextPoolable.SetPool(IDbContextPool)
IDbContextPoolable.SnapshotConfiguration()
IDbSetCache.GetOrAddSet(IDbSetSource, Type)

This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

Extension Methods

GetInfrastructure<T>(IInfrastructure<T>)
GetService<TService>(IInfrastructure<IServiceProvider>)
GetDependencies(IDbContextDependencies)
AsConcreteMetadataType<TInterface,TConcrete>(TInterface, String)