DbSet<TEntity> DbSet(Of TEntity) Class

Definition

A DbSet<TEntity> can be used to query and save instances of TEntity. LINQ queries against a DbSet<TEntity> will be translated into queries against the database.

The results of a LINQ query against a DbSet<TEntity> will contain the results returned from the database and may not reflect changes made in the context that have not been persisted to the database. For example, the results will not contain newly added entities and may still contain entities that are marked for deletion.

Depending on the database being used, some parts of a LINQ query against a DbSet<TEntity> may be evaluated in memory rather than being translated into a database query.

DbSet<TEntity> objects are usually obtained from a DbSet<TEntity> property on a derived DbContext or from the Set<TEntity>() method.

public abstract class DbSet<TEntity> : IListSource, IQueryable<TEntity>, IEnumerable<TEntity>, IQueryable, IEnumerable, IAsyncEnumerableAccessor<TEntity>, IInfrastructure<IServiceProvider> where TEntity : class
Public MustInherit Class DbSet(Of TEntity As Class)
    Inherits IListSource
    Implements IQueryable(Of TEntity), IEnumerable(Of TEntity), IQueryable, IEnumerable, IAsyncEnumerableAccessor(Of TEntity), IInfrastructure(Of IServiceProvider)
Type Parameters
TEntity

The type of entity being operated on by this set.

Inheritance
System.ObjectSystem.Object
DbSet<TEntity>DbSet(Of TEntity)
Implements
System.Linq.IQueryable<TEntity>System.Linq.IQueryable(Of TEntity) System.Collections.Generic.IEnumerable<TEntity>System.Collections.Generic.IEnumerable(Of TEntity) System.Linq.IQueryableSystem.Linq.IQueryable System.Collections.IEnumerableSystem.Collections.IEnumerable IAsyncEnumerableAccessor<TEntity>IAsyncEnumerableAccessor(Of TEntity) IInfrastructure<System.IServiceProvider>IInfrastructure(Of System.IServiceProvider)

Properties

Local Local

Gets an LocalView<TEntity> that represents a local view of all Added, Unchanged, and Modified entities in this set.

This local view will stay in sync as entities are added or removed from the context. Likewise, entities added to or removed from the local view will automatically be added to or removed from the context.

This property can be used for data binding by populating the set with data, for example by using the Load<TSource>(IQueryable<TSource>) extension method, and then binding to the local data through this property. For WPF bind to this property directly. For Windows Forms bind to the result of calling ToBindingList on this property.

public virtual LocalView<TEntity> Local { get; }
Public Overridable ReadOnly Property Local As LocalView(Of TEntity)
Value
LocalView<TEntity> LocalView(Of TEntity)

Methods

Add(TEntity) Add(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 entity)
Public Overridable Function Add(entity As TEntity) As EntityEntry(Of TEntity)
Parameters
entity
TEntity TEntity

The entity to add.

Returns
EntityEntry<TEntity> EntityEntry(Of TEntity)

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

AddAsync(TEntity, CancellationToken) AddAsync(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 entity, CancellationToken cancellationToken = null)
Public Overridable Function AddAsync(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<EntityEntry<TEntity>> System.Threading.Tasks.Task(Of EntityEntry(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(TEntity[]) AddRange(TEntity())

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 TEntity[] entities)
Public Overridable Sub AddRange(ParamArray entities As TEntity())
Parameters
entities
TEntity[] TEntity()

The entities to add.

AddRange(IEnumerable) AddRange(IEnumerable(Of TEntity))

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<TEntity> entities)
Public Overridable Sub AddRange(entities As IEnumerable(Of TEntity))
Parameters
entities
System.Collections.Generic.IEnumerable<TEntity> System.Collections.Generic.IEnumerable(Of TEntity)

The entities to add.

AddRangeAsync(TEntity[]) AddRangeAsync(TEntity())

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.

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 TEntity[] entities)
Public Overridable Function AddRangeAsync(ParamArray entities As TEntity()) As Task
Parameters
entities
TEntity[] TEntity()

The entities to add.

Returns
System.Threading.Tasks.Task System.Threading.Tasks.Task

A task that represents the asynchronous operation.

AddRangeAsync(IEnumerable, CancellationToken) AddRangeAsync(IEnumerable(Of TEntity), CancellationToken)

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.

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<TEntity> entities, CancellationToken cancellationToken = null)
Public Overridable Function AddRangeAsync(entities As IEnumerable(Of TEntity), cancellationToken As CancellationToken = Nothing) As Task
Parameters
entities
System.Collections.Generic.IEnumerable<TEntity> System.Collections.Generic.IEnumerable(Of TEntity)

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.

Attach(TEntity) Attach(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 entity)
Public Overridable Function Attach(entity As TEntity) As EntityEntry(Of TEntity)
Parameters
entity
TEntity TEntity

The entity to attach.

Returns
EntityEntry<TEntity> EntityEntry(Of TEntity)

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

AttachRange(TEntity[]) AttachRange(TEntity())

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 TEntity[] entities)
Public Overridable Sub AttachRange(ParamArray entities As TEntity())
Parameters
entities
TEntity[] TEntity()

The entities to attach.

AttachRange(IEnumerable) AttachRange(IEnumerable(Of TEntity))

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<TEntity> entities)
Public Overridable Sub AttachRange(entities As IEnumerable(Of TEntity))
Parameters
entities
System.Collections.Generic.IEnumerable<TEntity> System.Collections.Generic.IEnumerable(Of TEntity)

The entities to attach.

Find(Object[]) Find(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(params object[] keyValues)
Public Overridable Function Find(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(Object[]) FindAsync(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(params object[] keyValues)
Public Overridable Function FindAsync(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<TEntity> System.Threading.Tasks.Task(Of TEntity)

The entity found, or null.

FindAsync(Object[], CancellationToken) FindAsync(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(object[] keyValues, CancellationToken cancellationToken)
Public Overridable Function FindAsync(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<TEntity> System.Threading.Tasks.Task(Of TEntity)

The entity found, or null.

Remove(TEntity) Remove(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 entity)
Public Overridable Function Remove(entity As TEntity) As EntityEntry(Of TEntity)
Parameters
entity
TEntity TEntity

The entity to remove.

Returns
EntityEntry<TEntity> EntityEntry(Of TEntity)

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

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) was called before calling this method. This allows any cascading actions to be applied when SaveChanges() is called.

RemoveRange(TEntity[]) RemoveRange(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 void RemoveRange(params TEntity[] entities)
Public Overridable Sub RemoveRange(ParamArray entities As TEntity())
Parameters
entities
TEntity[] TEntity()

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(TEntity[]) was called before calling this method. This allows any cascading actions to be applied when SaveChanges() is called.

RemoveRange(IEnumerable) RemoveRange(IEnumerable(Of 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 void RemoveRange(IEnumerable<TEntity> entities)
Public Overridable Sub RemoveRange(entities As IEnumerable(Of TEntity))
Parameters
entities
System.Collections.Generic.IEnumerable<TEntity> System.Collections.Generic.IEnumerable(Of TEntity)

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<TEntity>) was called before calling this method. This allows any cascading actions to be applied when SaveChanges() is called.

Update(TEntity) Update(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) 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 entity)
Public Overridable Function Update(entity As TEntity) As EntityEntry(Of TEntity)
Parameters
entity
TEntity TEntity

The entity to update.

Returns
EntityEntry<TEntity> EntityEntry(Of TEntity)

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

UpdateRange(TEntity[]) UpdateRange(TEntity())

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(TEntity) 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 TEntity[] entities)
Public Overridable Sub UpdateRange(ParamArray entities As TEntity())
Parameters
entities
TEntity[] TEntity()

The entities to update.

UpdateRange(IEnumerable) UpdateRange(IEnumerable(Of TEntity))

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(TEntity) 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<TEntity> entities)
Public Overridable Sub UpdateRange(entities As IEnumerable(Of TEntity))
Parameters
entities
System.Collections.Generic.IEnumerable<TEntity> System.Collections.Generic.IEnumerable(Of TEntity)

The entities to update.

Explicit Interface Implementations

IInfrastructure.Instance Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure.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

IAsyncEnumerableAccessor.AsyncEnumerable Microsoft.EntityFrameworkCore.Query.Internal.IAsyncEnumerableAccessor.AsyncEnumerable

Returns an which when enumerated will asynchronously execute the query against the database.

IAsyncEnumerable<TEntity> IAsyncEnumerableAccessor<TEntity>.AsyncEnumerable { get; }
ReadOnly WriteOnly Property Microsoft.EntityFrameworkCore.Query.Internal.IAsyncEnumerableAccessor<TEntity>.AsyncEnumerable As IAsyncEnumerable(Of TEntity) Implements IAsyncEnumerableAccessor(Of TEntity).AsyncEnumerable
Returns
IAsyncEnumerable<TEntity> IAsyncEnumerable(Of TEntity)

The query results.

IEnumerable.GetEnumerator() System.Collections.Generic.IEnumerable.GetEnumerator()

Returns an System.Collections.Generic.IEnumerator<T> which when enumerated will execute a query against the database to load all entities from the database.

IEnumerator<TEntity> IEnumerable<TEntity>.GetEnumerator()
Function System.Collections.Generic.IEnumerable<TEntity>.GetEnumerator As IEnumerator(Of TEntity) Implements IEnumerable(Of TEntity).GetEnumerator
Returns
System.Collections.Generic.IEnumerator<TEntity> System.Collections.Generic.IEnumerator(Of TEntity)

The query results.

IEnumerable.GetEnumerator() System.Collections.IEnumerable.GetEnumerator()

Returns an System.Collections.IEnumerator which when enumerated will execute a query against the database to load all entities from the database.

IEnumerator IEnumerable.GetEnumerator()
Function System.Collections.IEnumerable.GetEnumerator As IEnumerator Implements IEnumerable.GetEnumerator
Returns
System.Collections.IEnumerator System.Collections.IEnumerator

The query results.

IQueryable.ElementType System.Linq.IQueryable.ElementType

Gets the IQueryable element type.

Type IQueryable.ElementType { get; }
ReadOnly WriteOnly Property System.Linq.IQueryable.ElementType As Type Implements IQueryable.ElementType
Returns
System.Type System.Type

IQueryable.Expression System.Linq.IQueryable.Expression

Gets the IQueryable LINQ Expression.

Expression IQueryable.Expression { get; }
ReadOnly WriteOnly Property System.Linq.IQueryable.Expression As Expression Implements IQueryable.Expression
Returns
System.Linq.Expressions.Expression System.Linq.Expressions.Expression

IQueryable.Provider System.Linq.IQueryable.Provider

Gets the IQueryable provider.

IQueryProvider IQueryable.Provider { get; }
ReadOnly WriteOnly Property System.Linq.IQueryable.Provider As IQueryProvider Implements IQueryable.Provider
Returns
System.Linq.IQueryProvider System.Linq.IQueryProvider