DbContext DbContext Class

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.

Syntax

Declaration

public class DbContext : IDisposable, IInfrastructure<IServiceProvider>Public Class DbContext
    Implements IDisposable, IInfrastructure(Of IServiceProvider)

Inheritance Hierarchy

  • System.Object
    System.Object
  • DbContext
    DbContext

Inherited Members

ToString(),
ToString(),
Equals(Object),
Equals(Object),
Equals(Object, Object),
Equals(Object, Object),
ReferenceEquals(Object, Object),
ReferenceEquals(Object, Object),
GetHashCode(),
GetHashCode(),
GetType(),
GetType(),
MemberwiseClone()
MemberwiseClone()

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 summary

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.

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 summary

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

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

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

Methods summary

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Releases the allocated resources for this context.

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.

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

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 dataabse 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.

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 dataabse 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.

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 dataabse 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.

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 dataabse 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.

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 dataabse 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.

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 dataabse 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.

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).

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.

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

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

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

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

Saves all changes made in this context to the database.

Saves all changes made in this context to the database.

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

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

Creates a DbSet<TEntity> that can be used to query and save instances of 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(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.

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.

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.

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 summary

Gets the scoped System.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.

Constructors

  • DbContext()
    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.

    protected DbContext()Protected Sub New
  • DbContext(DbContextOptions)
    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.

    public DbContext(DbContextOptions options)Public Sub New(options As DbContextOptions)

    Parameters

Properties

  • ChangeTracker
    ChangeTracker

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

    public virtual ChangeTracker ChangeTracker { get; }Public Overridable ReadOnly Property ChangeTracker As ChangeTracker

    Property Value

  • Database
    Database

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

    public virtual DatabaseFacade Database { get; }Public Overridable ReadOnly Property Database As DatabaseFacade

    Property Value

  • Model
    Model

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

    public virtual IModel Model { get; }Public Overridable ReadOnly Property Model As IModel

    Property Value

Methods

  • Add(Object)
    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.

    public virtual EntityEntry Add(object entity)Public Overridable Function Add(entity As Object) As EntityEntry

    Parameters

    • entity
      System.Object
      System.Object

      The entity to add.

    Returns

  • Add<TEntity>(TEntity)
    Add(Of 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.

    public virtual EntityEntry<TEntity> Add<TEntity>(TEntity entity)where TEntity : classPublic Overridable Function Add(Of TEntity As Class)(entity As TEntity) As EntityEntry(Of TEntity)

    Parameters

    • entity
      TEntity
      TEntity

      The entity to add.

    Returns

  • AddAsync(Object, CancellationToken)
    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.

    public virtual Task<EntityEntry> AddAsync(object entity, CancellationToken cancellationToken = null)Public Overridable Function AddAsync(entity As Object, cancellationToken As CancellationToken = Nothing) As Task(Of EntityEntry)

    Parameters

    • entity
      System.Object
      System.Object

      The entity to add.

    • cancellationToken
      System.Threading.CancellationToken
      System.Threading.CancellationToken

      A System.Threading.CancellationToken to observe while waiting for the task to complete.

    Returns

    • System.Threading.Tasks.Task<TResult><EntityEntry>
      System.Threading.Tasks.Task<TResult>(Of EntityEntry)

      A task that represents the asynchronous Add operation. The task result contains the

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

  • AddAsync<TEntity>(TEntity, CancellationToken)
    AddAsync(Of 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.

    public virtual Task<EntityEntry<TEntity>> AddAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = null)where TEntity : classPublic Overridable Function AddAsync(Of TEntity As Class)(entity As TEntity, cancellationToken As CancellationToken = Nothing) As Task(Of EntityEntry(Of TEntity))

    Parameters

    • entity
      TEntity
      TEntity

      The entity to add.

    • cancellationToken
      System.Threading.CancellationToken
      System.Threading.CancellationToken

      A System.Threading.CancellationToken to observe while waiting for the task to complete.

    Returns

    • System.Threading.Tasks.Task<TResult><EntityEntry<TEntity><TEntity>>
      System.Threading.Tasks.Task<TResult>(Of EntityEntry<TEntity>(Of TEntity))

      A task that represents the asynchronous Add operation. The task result contains the

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

  • AddRange(IEnumerable<Object>)
    AddRange(IEnumerable(Of 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.

    public virtual void AddRange(IEnumerable<object> entities)Public Overridable Sub AddRange(entities As IEnumerable(Of Object))

    Parameters

    • entities
      System.Collections.Generic.IEnumerable<T><System.Object>
      System.Collections.Generic.IEnumerable<T>(Of System.Object)

      The entities to add.

  • AddRange(Object[])
    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.

    public virtual void AddRange(params object[] entities)Public Overridable Sub AddRange(ParamArray entities As Object())

    Parameters

    • entities
      System.Object[]
      System.Object()

      The entities to add.

  • AddRangeAsync(IEnumerable<Object>, CancellationToken)
    AddRangeAsync(IEnumerable(Of 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.

    public virtual Task AddRangeAsync(IEnumerable<object> entities, CancellationToken cancellationToken = null)Public Overridable Function AddRangeAsync(entities As IEnumerable(Of Object), cancellationToken As CancellationToken = Nothing) As Task

    Parameters

    • entities
      System.Collections.Generic.IEnumerable<T><System.Object>
      System.Collections.Generic.IEnumerable<T>(Of System.Object)

      The entities to add.

    • cancellationToken
      System.Threading.CancellationToken
      System.Threading.CancellationToken

      A System.Threading.CancellationToken to observe while waiting for the task to complete.

    Returns

    • System.Threading.Tasks.Task
      System.Threading.Tasks.Task

      A task that represents the asynchronous operation.

  • AddRangeAsync(Object[])
    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.

    public virtual Task AddRangeAsync(params object[] entities)Public Overridable Function AddRangeAsync(ParamArray entities As Object()) As Task

    Parameters

    • entities
      System.Object[]
      System.Object()

      The entities to add.

    Returns

    • System.Threading.Tasks.Task
      System.Threading.Tasks.Task

      A task that represents the asynchronous operation.

  • Attach(Object)
    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.

    public virtual EntityEntry Attach(object entity)Public Overridable Function Attach(entity As Object) As EntityEntry

    Parameters

    • entity
      System.Object
      System.Object

      The entity to attach.

    Returns

  • Attach<TEntity>(TEntity)
    Attach(Of 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.

    public virtual EntityEntry<TEntity> Attach<TEntity>(TEntity entity)where TEntity : classPublic Overridable Function Attach(Of TEntity As Class)(entity As TEntity) As EntityEntry(Of TEntity)

    Parameters

    • entity
      TEntity
      TEntity

      The entity to attach.

    Returns

  • AttachRange(IEnumerable<Object>)
    AttachRange(IEnumerable(Of 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.

    public virtual void AttachRange(IEnumerable<object> entities)Public Overridable Sub AttachRange(entities As IEnumerable(Of Object))

    Parameters

    • entities
      System.Collections.Generic.IEnumerable<T><System.Object>
      System.Collections.Generic.IEnumerable<T>(Of System.Object)

      The entities to attach.

  • AttachRange(Object[])
    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.

    public virtual void AttachRange(params object[] entities)Public Overridable Sub AttachRange(ParamArray entities As Object())

    Parameters

    • entities
      System.Object[]
      System.Object()

      The entities to attach.

  • Dispose()
    Dispose()

    Releases the allocated resources for this context.

    public virtual void Dispose()Public Overridable Sub Dispose
  • Entry(Object)
    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.

    public virtual EntityEntry Entry(object entity)Public Overridable Function Entry(entity As Object) As EntityEntry

    Parameters

    • entity
      System.Object
      System.Object

      The entity to get the entry for.

    Returns

  • Entry<TEntity>(TEntity)
    Entry(Of TEntity)(TEntity)

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

    public virtual EntityEntry<TEntity> Entry<TEntity>(TEntity entity)where TEntity : classPublic Overridable Function Entry(Of TEntity As Class)(entity As TEntity) As EntityEntry(Of TEntity)

    Parameters

    • entity
      TEntity
      TEntity

      The entity to get the entry for.

    Returns

  • Find(Type, Object[])
    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 dataabse 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.

    public virtual object Find(Type entityType, params object[] keyValues)Public Overridable Function Find(entityType As Type, ParamArray keyValues As Object()) As Object

    Parameters

    • entityType
      System.Type
      System.Type

      The type of entity to find.

    • keyValues
      System.Object[]
      System.Object()

      The values of the primary key for the entity to be found.

    Returns

    • System.Object
      System.Object

      The entity found, or null.

  • Find<TEntity>(Object[])
    Find(Of 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 dataabse 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.

    public virtual TEntity Find<TEntity>(params object[] keyValues)where TEntity : classPublic Overridable Function Find(Of TEntity As Class)(ParamArray keyValues As Object()) As TEntity

    Parameters

    • keyValues
      System.Object[]
      System.Object()

      The values of the primary key for the entity to be found.

    Returns

    • TEntity
      TEntity

      The entity found, or null.

  • FindAsync(Type, Object[])
    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 dataabse 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.

    public virtual Task<object> FindAsync(Type entityType, params object[] keyValues)Public Overridable Function FindAsync(entityType As Type, ParamArray keyValues As Object()) As Task(Of Object)

    Parameters

    • entityType
      System.Type
      System.Type

      The type of entity to find.

    • keyValues
      System.Object[]
      System.Object()

      The values of the primary key for the entity to be found.

    Returns

    • System.Threading.Tasks.Task<TResult><System.Object>
      System.Threading.Tasks.Task<TResult>(Of System.Object)

      The entity found, or null.

  • FindAsync(Type, Object[], CancellationToken)
    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 dataabse 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.

    public virtual Task<object> FindAsync(Type entityType, object[] keyValues, CancellationToken cancellationToken)Public Overridable Function FindAsync(entityType As Type, keyValues As Object(), cancellationToken As CancellationToken) As Task(Of Object)

    Parameters

    • entityType
      System.Type
      System.Type

      The type of entity to find.

    • keyValues
      System.Object[]
      System.Object()

      The values of the primary key for the entity to be found.

    • cancellationToken
      System.Threading.CancellationToken
      System.Threading.CancellationToken

      A System.Threading.CancellationToken to observe while waiting for the task to complete.

    Returns

    • System.Threading.Tasks.Task<TResult><System.Object>
      System.Threading.Tasks.Task<TResult>(Of System.Object)

      The entity found, or null.

  • FindAsync<TEntity>(Object[])
    FindAsync(Of 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 dataabse 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.

    public virtual Task<TEntity> FindAsync<TEntity>(params object[] keyValues)where TEntity : classPublic Overridable Function FindAsync(Of TEntity As Class)(ParamArray keyValues As Object()) As Task(Of TEntity)

    Parameters

    • keyValues
      System.Object[]
      System.Object()

      The values of the primary key for the entity to be found.

    Returns

    • System.Threading.Tasks.Task<TResult><TEntity>
      System.Threading.Tasks.Task<TResult>(Of TEntity)

      The entity found, or null.

  • FindAsync<TEntity>(Object[], CancellationToken)
    FindAsync(Of 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 dataabse 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.

    public virtual Task<TEntity> FindAsync<TEntity>(object[] keyValues, CancellationToken cancellationToken)where TEntity : classPublic Overridable Function FindAsync(Of TEntity As Class)(keyValues As Object(), cancellationToken As CancellationToken) As Task(Of TEntity)

    Parameters

    • keyValues
      System.Object[]
      System.Object()

      The values of the primary key for the entity to be found.

    • cancellationToken
      System.Threading.CancellationToken
      System.Threading.CancellationToken

      A System.Threading.CancellationToken to observe while waiting for the task to complete.

    Returns

    • System.Threading.Tasks.Task<TResult><TEntity>
      System.Threading.Tasks.Task<TResult>(Of TEntity)

      The entity found, or null.

  • OnConfiguring(DbContextOptionsBuilder)
    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).

    protected virtual void OnConfiguring(DbContextOptionsBuilder optionsBuilder)Protected Overridable Sub OnConfiguring(optionsBuilder As DbContextOptionsBuilder)

    Parameters

    • optionsBuilder

      A builder used to create or modify options for this context. Databases (and other extensions) typically define extension methods on this object that allow you to configure the context.

  • OnModelCreating(ModelBuilder)
    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.

    protected virtual void OnModelCreating(ModelBuilder modelBuilder)Protected Overridable Sub OnModelCreating(modelBuilder As ModelBuilder)

    Parameters

    • modelBuilder

      The builder being used to construct the model for this context. Databases (and other extensions) typically define extension methods on this object that allow you to configure aspects of the model that are specific to a given database.

    Remarks

    If a model is explicitly set on the options for this context (via UseModel(IModel)) then this method will not be run.

  • Remove(Object)
    Remove(Object)

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

    public virtual EntityEntry Remove(object entity)Public Overridable Function Remove(entity As Object) As EntityEntry

    Parameters

    • entity
      System.Object
      System.Object

      The entity to remove.

    Returns

    Remarks

    If the entity is already tracked in the Added state then the context will stop tracking the entity (rather than marking it as Deleted) since the entity was previously added to the context and does not exist in the database.

    Any other reachable entities that are not already being tracked will be tracked in the same way that they would be if Attach(Object) was called before calling this method. This allows any cascading actions to be applied when SaveChanges() is called.

  • Remove<TEntity>(TEntity)
    Remove(Of TEntity)(TEntity)

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

    public virtual EntityEntry<TEntity> Remove<TEntity>(TEntity entity)where TEntity : classPublic Overridable Function Remove(Of TEntity As Class)(entity As TEntity) As EntityEntry(Of TEntity)

    Parameters

    • entity
      TEntity
      TEntity

      The entity to remove.

    Returns

    Remarks

    If the entity is already tracked in the Added state then the context will stop tracking the entity (rather than marking it as Deleted) since the entity was previously added to the context and does not exist in the database.

    Any other reachable entities that are not already being tracked will be tracked in the same way that they would be if Attach<TEntity>(TEntity) was called before calling this method. This allows any cascading actions to be applied when SaveChanges() is called.

  • RemoveRange(IEnumerable<Object>)
    RemoveRange(IEnumerable(Of Object))

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

    public virtual void RemoveRange(IEnumerable<object> entities)Public Overridable Sub RemoveRange(entities As IEnumerable(Of Object))

    Parameters

    • entities
      System.Collections.Generic.IEnumerable<T><System.Object>
      System.Collections.Generic.IEnumerable<T>(Of System.Object)

      The entities to remove.

    Remarks

    If any of the entities are already tracked in the Added state then the context will stop tracking those entities (rather than marking them as Deleted) since those entities were previously added to the context and do not exist in the database.

    Any other reachable entities that are not already being tracked will be tracked in the same way that they would be if AttachRange(IEnumerable<Object>) was called before calling this method. This allows any cascading actions to be applied when SaveChanges() is called.

  • RemoveRange(Object[])
    RemoveRange(Object())

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

    public virtual void RemoveRange(params object[] entities)Public Overridable Sub RemoveRange(ParamArray entities As Object())

    Parameters

    • entities
      System.Object[]
      System.Object()

      The entities to remove.

    Remarks

    If any of the entities are already tracked in the Added state then the context will stop tracking those entities (rather than marking them as Deleted) since those entities were previously added to the context and do not exist in the database.

    Any other reachable entities that are not already being tracked will be tracked in the same way that they would be if AttachRange(Object[]) was called before calling this method. This allows any cascading actions to be applied when SaveChanges() is called.

  • SaveChanges()
    SaveChanges()

    Saves all changes made in this context to the database.

    public virtual int SaveChanges()Public Overridable Function SaveChanges As Integer

    Returns

    • System.Int32
      System.Int32

      The number of state entries written to the database.

    Remarks

    This method will automatically call DetectChanges() to discover any changes to entity instances before saving to the underlying database. This can be disabled via

    AutoDetectChangesEnabled.

  • SaveChanges(Boolean)
    SaveChanges(Boolean)

    Saves all changes made in this context to the database.

    public virtual int SaveChanges(bool acceptAllChangesOnSuccess)Public Overridable Function SaveChanges(acceptAllChangesOnSuccess As Boolean) As Integer

    Parameters

    • acceptAllChangesOnSuccess
      System.Boolean
      System.Boolean

      Indicates whether AcceptAllChanges() is called after the changes have been sent successfully to the database.

    Returns

    • System.Int32
      System.Int32

      The number of state entries written to the database.

    Remarks

    This method will automatically call DetectChanges() to discover any changes to entity instances before saving to the underlying database. This can be disabled via

    AutoDetectChangesEnabled.

  • SaveChangesAsync(Boolean, CancellationToken)
    SaveChangesAsync(Boolean, CancellationToken)

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

    public virtual Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = null)Public Overridable Function SaveChangesAsync(acceptAllChangesOnSuccess As Boolean, cancellationToken As CancellationToken = Nothing) As Task(Of Integer)

    Parameters

    • acceptAllChangesOnSuccess
      System.Boolean
      System.Boolean

      Indicates whether AcceptAllChanges() is called after the changes have been sent successfully to the database.

    • cancellationToken
      System.Threading.CancellationToken
      System.Threading.CancellationToken

      A System.Threading.CancellationToken to observe while waiting for the task to complete.

    Returns

    • System.Threading.Tasks.Task<TResult><System.Int32>
      System.Threading.Tasks.Task<TResult>(Of System.Int32)

      A task that represents the asynchronous save operation. The task result contains the number of state entries written to the database.

    Remarks

    This method will automatically call DetectChanges() to discover any changes to entity instances before saving to the underlying database. This can be disabled via AutoDetectChangesEnabled.

    Multiple active operations on the same context instance are not supported. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context.

  • SaveChangesAsync(CancellationToken)
    SaveChangesAsync(CancellationToken)

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

    public virtual Task<int> SaveChangesAsync(CancellationToken cancellationToken = null)Public Overridable Function SaveChangesAsync(cancellationToken As CancellationToken = Nothing) As Task(Of Integer)

    Parameters

    • cancellationToken
      System.Threading.CancellationToken
      System.Threading.CancellationToken

      A System.Threading.CancellationToken to observe while waiting for the task to complete.

    Returns

    • System.Threading.Tasks.Task<TResult><System.Int32>
      System.Threading.Tasks.Task<TResult>(Of System.Int32)

      A task that represents the asynchronous save operation. The task result contains the number of state entries written to the database.

    Remarks

    This method will automatically call DetectChanges() to discover any changes to entity instances before saving to the underlying database. This can be disabled via AutoDetectChangesEnabled.

    Multiple active operations on the same context instance are not supported. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context.

  • Set<TEntity>()
    Set(Of TEntity)()

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

    public virtual DbSet<TEntity> Set<TEntity>()where TEntity : classPublic Overridable Function Set(Of TEntity As Class) As DbSet(Of TEntity)

    Returns

  • Update(Object)
    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.

    public virtual EntityEntry Update(object entity)Public Overridable Function Update(entity As Object) As EntityEntry

    Parameters

    • entity
      System.Object
      System.Object

      The entity to update.

    Returns

  • Update<TEntity>(TEntity)
    Update(Of 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.

    public virtual EntityEntry<TEntity> Update<TEntity>(TEntity entity)where TEntity : classPublic Overridable Function Update(Of TEntity As Class)(entity As TEntity) As EntityEntry(Of TEntity)

    Parameters

    • entity
      TEntity
      TEntity

      The entity to update.

    Returns

  • UpdateRange(IEnumerable<Object>)
    UpdateRange(IEnumerable(Of 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.

    public virtual void UpdateRange(IEnumerable<object> entities)Public Overridable Sub UpdateRange(entities As IEnumerable(Of Object))

    Parameters

    • entities
      System.Collections.Generic.IEnumerable<T><System.Object>
      System.Collections.Generic.IEnumerable<T>(Of System.Object)

      The entities to update.

  • UpdateRange(Object[])
    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.

    public virtual void UpdateRange(params object[] entities)Public Overridable Sub UpdateRange(ParamArray entities As Object())

    Parameters

    • entities
      System.Object[]
      System.Object()

      The entities to update.

Explicit Interface Implementations

  • IInfrastructure<IServiceProvider>.Instance
    Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.Instance

    Gets the scoped System.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.

    IServiceProvider IInfrastructure<IServiceProvider>.Instance { get; }ReadOnly WriteOnly Property Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.Instance As IServiceProvider Implements IInfrastructure(Of IServiceProvider).Instance

    Returns

    • System.IServiceProvider
      System.IServiceProvider

Details

Assembly

Microsoft.EntityFrameworkCore.dll