DbSet<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> : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<IServiceProvider>, Microsoft.EntityFrameworkCore.Query.Internal.IAsyncEnumerableAccessor<TEntity>, System.Collections.Generic.IEnumerable<TEntity>, System.ComponentModel.IListSource, System.Linq.IQueryable<TEntity> where TEntity : class
Type Parameters
TEntity

The type of entity being operated on by this set.

Inheritance
DbSet<TEntity>
Derived
Implements

Constructors

DbSet<TEntity>()

Properties

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 by calling ToObservableCollection() for WPF binding, or ToBindingList() for WinForms.

Methods

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.

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.

AddRange(IEnumerable<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.

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.

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.

AddRangeAsync(IEnumerable<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.

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.

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.

AttachRange(IEnumerable<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.

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

Remove(TEntity)

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

RemoveRange(TEntity[])

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

RemoveRange(IEnumerable<TEntity>)

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

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.

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.

UpdateRange(IEnumerable<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.

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.

IAsyncEnumerableAccessor<TEntity>.AsyncEnumerable

Returns an System.Collections.Generic.IAsyncEnumerable which when enumerated will asynchronously execute the query against the database.

IEnumerable<TEntity>.GetEnumerator()

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

IEnumerable.GetEnumerator()

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

IListSource.ContainsListCollection

Gets a value indicating whether the collection is a collection of System.Collections.IList objects. Always returns false.

IListSource.GetList()

This method is called by data binding frameworks when attempting to data bind directly to a DbSet<TEntity>.

This implementation always throws an exception as binding directly to a DbSet<TEntity> will result in a query being sent to the database every time the data binding framework requests the contents of the collection. Instead load the results into the context, for example, by using the Load<TSource>(IQueryable<TSource>) extension method, and then bind to the local data through the Local by calling ToObservableCollection() for WPF binding, or ToBindingList() for WinForms.

IQueryable.ElementType

Gets the IQueryable element type.

IQueryable.Expression

Gets the IQueryable LINQ Expression.

IQueryable.Provider

Gets the IQueryable provider.

Extension Methods

AllAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)
AnyAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)
AnyAsync<TSource>(IQueryable<TSource>, CancellationToken)
AsNoTracking<TEntity>(IQueryable<TEntity>)
AsTracking<TEntity>(IQueryable<TEntity>)
AverageAsync(IQueryable<Decimal>, CancellationToken)
AverageAsync(IQueryable<Double>, CancellationToken)
AverageAsync(IQueryable<Int32>, CancellationToken)
AverageAsync(IQueryable<Int64>, CancellationToken)
AverageAsync(IQueryable<Nullable<Decimal>>, CancellationToken)
AverageAsync(IQueryable<Nullable<Double>>, CancellationToken)
AverageAsync(IQueryable<Nullable<Int32>>, CancellationToken)
AverageAsync(IQueryable<Nullable<Int64>>, CancellationToken)
AverageAsync(IQueryable<Nullable<Single>>, CancellationToken)
AverageAsync(IQueryable<Single>, CancellationToken)
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Decimal>>, CancellationToken)
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Double>>, CancellationToken)
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>, CancellationToken)
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int64>>, CancellationToken)
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Decimal>>>, CancellationToken)
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Double>>>, CancellationToken)
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int32>>>, CancellationToken)
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int64>>>, CancellationToken)
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Single>>>, CancellationToken)
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Single>>, CancellationToken)
ContainsAsync<TSource>(IQueryable<TSource>, TSource, CancellationToken)
CountAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)
CountAsync<TSource>(IQueryable<TSource>, CancellationToken)
FirstAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)
FirstAsync<TSource>(IQueryable<TSource>, CancellationToken)
FirstOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)
FirstOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken)
ForEachAsync<T>(IQueryable<T>, Action<T>, CancellationToken)
IgnoreQueryFilters<TEntity>(IQueryable<TEntity>)
Include<TEntity>(IQueryable<TEntity>, String)
Include<TEntity,TProperty>(IQueryable<TEntity>, Expression<Func<TEntity,TProperty>>)
LastAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)
LastAsync<TSource>(IQueryable<TSource>, CancellationToken)
LastOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)
LastOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken)
Load<TSource>(IQueryable<TSource>)
LoadAsync<TSource>(IQueryable<TSource>, CancellationToken)
LongCountAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)
LongCountAsync<TSource>(IQueryable<TSource>, CancellationToken)
MaxAsync<TSource>(IQueryable<TSource>, CancellationToken)
MaxAsync<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>, CancellationToken)
MinAsync<TSource>(IQueryable<TSource>, CancellationToken)
MinAsync<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>, CancellationToken)
SingleAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)
SingleAsync<TSource>(IQueryable<TSource>, CancellationToken)
SingleOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)
SingleOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken)
SumAsync(IQueryable<Decimal>, CancellationToken)
SumAsync(IQueryable<Double>, CancellationToken)
SumAsync(IQueryable<Int32>, CancellationToken)
SumAsync(IQueryable<Int64>, CancellationToken)
SumAsync(IQueryable<Nullable<Decimal>>, CancellationToken)
SumAsync(IQueryable<Nullable<Double>>, CancellationToken)
SumAsync(IQueryable<Nullable<Int32>>, CancellationToken)
SumAsync(IQueryable<Nullable<Int64>>, CancellationToken)
SumAsync(IQueryable<Nullable<Single>>, CancellationToken)
SumAsync(IQueryable<Single>, CancellationToken)
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Decimal>>, CancellationToken)
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Double>>, CancellationToken)
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>, CancellationToken)
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int64>>, CancellationToken)
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Decimal>>>, CancellationToken)
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Double>>>, CancellationToken)
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int32>>>, CancellationToken)
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int64>>>, CancellationToken)
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Single>>>, CancellationToken)
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Single>>, CancellationToken)
ToArrayAsync<TSource>(IQueryable<TSource>, CancellationToken)
ToDictionaryAsync<TSource,TKey>(IQueryable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>, CancellationToken)
ToDictionaryAsync<TSource,TKey>(IQueryable<TSource>, Func<TSource,TKey>, CancellationToken)
ToDictionaryAsync<TSource,TKey,TElement>(IQueryable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>, CancellationToken)
ToDictionaryAsync<TSource,TKey,TElement>(IQueryable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, CancellationToken)
ToListAsync<TSource>(IQueryable<TSource>, CancellationToken)
AsAsyncEnumerable<TSource>(IQueryable<TSource>)
Select<TSource,TResult>(IQueryable<TSource>, String)
GetInfrastructure<T>(IInfrastructure<T>)
GetService<TService>(IInfrastructure<IServiceProvider>)
Distinct<T>(IEnumerable<T>, Func<T,T,Boolean>)
Join(IEnumerable<Object>, String)
OrderByOrdinal<TSource>(IEnumerable<TSource>, Func<TSource,String>)
StartsWith<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
StructuralSequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
FormatColumns(IEnumerable<IProperty>)
AsConcreteMetadataType<TInterface,TConcrete>(TInterface, String)
FromSql<TEntity>(IQueryable<TEntity>, RawSqlString, Object[])
FromSql<TEntity>(IQueryable<TEntity>, FormattableString)
FromSql<TEntity>(IQueryable<TEntity>, String, Object[])