DbSet<TEntity> 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
A DbSet<TEntity> 可用于查询和保存 . TEntity的实例。
针对 a DbSet<TEntity> 的 LINQ 查询将转换为针对数据库的查询。
public abstract class DbSet<TEntity> : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<IServiceProvider>, Microsoft.EntityFrameworkCore.Query.Internal.IAsyncEnumerableAccessor<TEntity>, System.Collections.Generic.IEnumerable<TEntity>, System.Linq.IQueryable<TEntity> where TEntity : class
[System.Runtime.CompilerServices.Nullable(0)]
public abstract class DbSet<TEntity> : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<IServiceProvider>, System.Collections.Generic.IEnumerable<TEntity>, System.ComponentModel.IListSource, System.Linq.IQueryable<TEntity> where TEntity : class
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
public abstract class DbSet<TEntity> : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<IServiceProvider>, System.Collections.Generic.IAsyncEnumerable<TEntity>, System.Collections.Generic.IEnumerable<TEntity>, System.ComponentModel.IListSource, System.Linq.IQueryable<TEntity> where TEntity : class
public abstract class DbSet<TEntity> : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<IServiceProvider>, System.Collections.Generic.IEnumerable<TEntity>, System.ComponentModel.IListSource, System.Linq.IQueryable<TEntity> where TEntity : class
type DbSet<'Entity (requires 'Entity : null)> = class
interface IQueryable<'Entity (requires 'Entity : null)>
interface seq<'Entity (requires 'Entity : null)>
interface IEnumerable
interface IQueryable
interface IAsyncEnumerableAccessor<'Entity (requires 'Entity : null)>
interface IInfrastructure<IServiceProvider>
[<System.Runtime.CompilerServices.Nullable(0)>]
type DbSet<'Entity (requires 'Entity : null)> = class
interface IQueryable<'Entity (requires 'Entity : null)>
interface seq<'Entity (requires 'Entity : null)>
interface IEnumerable
interface IQueryable
interface IInfrastructure<IServiceProvider>
interface IListSource
type DbSet<'Entity (requires 'Entity : null)> = class
interface IQueryable<'Entity (requires 'Entity : null)>
interface seq<'Entity (requires 'Entity : null)>
interface IEnumerable
interface IQueryable
interface IAsyncEnumerableAccessor<'Entity (requires 'Entity : null)>
interface IInfrastructure<IServiceProvider>
interface IListSource
type DbSet<'Entity (requires 'Entity : null)> = class
interface IQueryable<'Entity (requires 'Entity : null)>
interface seq<'Entity (requires 'Entity : null)>
interface IEnumerable
interface IQueryable
interface IAsyncEnumerable<'Entity (requires 'Entity : null)>
interface IInfrastructure<IServiceProvider>
interface IListSource
type DbSet<'Entity (requires 'Entity : null)> = class
interface IQueryable<'Entity (requires 'Entity : null)>
interface seq<'Entity (requires 'Entity : null)>
interface IEnumerable
interface IQueryable
interface IInfrastructure<IServiceProvider>
interface IListSource
Public MustInherit Class DbSet(Of TEntity)
Implements IAsyncEnumerableAccessor(Of TEntity), IEnumerable(Of TEntity), IInfrastructure(Of IServiceProvider), IQueryable(Of TEntity)
Public MustInherit Class DbSet(Of TEntity)
Implements IEnumerable(Of TEntity), IInfrastructure(Of IServiceProvider), IListSource, IQueryable(Of TEntity)
Public MustInherit Class DbSet(Of TEntity)
Implements IAsyncEnumerableAccessor(Of TEntity), IEnumerable(Of TEntity), IInfrastructure(Of IServiceProvider), IListSource, IQueryable(Of TEntity)
Public MustInherit Class DbSet(Of TEntity)
Implements IAsyncEnumerable(Of TEntity), IEnumerable(Of TEntity), IInfrastructure(Of IServiceProvider), IListSource, IQueryable(Of TEntity)
类型参数
- TEntity
此集所操作的实体的类型。
- 继承
-
DbSet<TEntity>
- 派生
- 特性
- 实现
-
IInfrastructure<IServiceProvider> Microsoft.EntityFrameworkCore.Query.Internal.IAsyncEnumerableAccessor<TEntity> IEnumerable<T> IEnumerable<TEntity> IEnumerable IQueryable IQueryable<TEntity> IListSource IAsyncEnumerable<TEntity>
注解
针对 a DbSet<TEntity> 的 LINQ 查询的结果将包含从数据库返回的结果,并且可能不会反映未保存到数据库的上下文中所做的更改。 例如,结果将不包含新添加的实体,并且可能仍包含标记为要删除的实体。
根据所使用的数据库,可能会根据内存中评估 LINQ 查询 DbSet<TEntity> 的某些部分,而不是转换为数据库查询。
DbSet<TEntity> 对象通常从 DbSet<TEntity> 派生 DbContext 或方法 Set<TEntity>() 上的属性获取。
Entity Framework Core 不支持在同一 DbContext 实例上运行多个并行操作。 这包括异步查询的并行执行以及从多个线程进行的任何显式并发使用。 因此,请始终立即等待异步调用,或者对并行执行的操作使用单独的 DbContext 实例。 有关详细信息和示例,请参阅 避免 DbContext 线程问题 。
有关详细信息和示例,请参阅 DbContext 生存期、配置和初始化、 使用 EF Core 查询数据以及 更改跟踪 。
构造函数
| 名称 | 说明 |
|---|---|
| DbSet<TEntity>() |
A DbSet<TEntity> 可用于查询和保存 . |
属性
| 名称 | 说明 |
|---|---|
| EntityType |
IEntityType与此集关联的元数据。 |
| Local |
获取一个 LocalView<TEntity> 表示此集中所有已添加、未更改和已修改实体的本地视图。 |
方法
| 名称 | 说明 |
|---|---|
| Add(TEntity) |
开始跟踪给定实体,以及尚未跟踪的任何其他可访问实体, Added 以便在调用时 SaveChanges() 将其插入数据库。 |
| AddAsync(TEntity, CancellationToken) |
开始跟踪给定实体,以及尚未跟踪的任何其他可访问实体, Added 以便在调用时 SaveChanges() 将其插入数据库。 |
| AddRange(IEnumerable<TEntity>) |
开始跟踪给定的实体,以及尚未跟踪的任何其他可访问实体, Added 处于这样的状态,以便在调用时 SaveChanges() 将其插入数据库。 |
| AddRange(TEntity[]) |
开始跟踪给定的实体,以及尚未跟踪的任何其他可访问实体, Added 处于这样的状态,以便在调用时 SaveChanges() 将其插入数据库。 |
| AddRangeAsync(IEnumerable<TEntity>, CancellationToken) |
开始跟踪给定的实体,以及尚未跟踪的任何其他可访问实体, Added 处于这样的状态,以便在调用时 SaveChanges() 将其插入数据库。 |
| AddRangeAsync(TEntity[]) |
开始跟踪给定的实体,以及尚未跟踪的任何其他可访问实体, Added 处于这样的状态,以便在调用时 SaveChanges() 将其插入数据库。 |
| AsAsyncEnumerable() |
返回类型化为 IAsyncEnumerable<T>. |
| AsQueryable() |
返回类型化为 IQueryable<T>. |
| Attach(TEntity) |
开始跟踪给定实体和默认情况下可以使用状态 Unchanged 从给定实体访问的条目,但有关使用不同状态的情况,请参阅下文。 |
| AttachRange(IEnumerable<TEntity>) |
开始跟踪默认使用 Unchanged 状态从给定实体访问的给定实体和条目,但有关使用不同状态的情况,请参阅下文。 |
| AttachRange(TEntity[]) |
开始跟踪默认使用 Unchanged 状态从给定实体访问的给定实体和条目,但有关使用不同状态的情况,请参阅下文。 |
| Entry(TEntity) |
获取给定实体的一个 EntityEntry<TEntity> 。 该条目提供对实体的更改跟踪信息和操作的访问权限。 |
| Find(Object[]) |
查找具有给定主键值的实体。 如果上下文正在跟踪具有给定主键值的实体,则会立即返回该实体,而无需向数据库发出请求。 否则,将为具有给定主键值的实体的数据库进行查询,如果找到该实体,则会附加到上下文并返回该实体。 如果未找到任何实体,则返回 null。 |
| FindAsync(Object[], CancellationToken) |
查找具有给定主键值的实体。 如果上下文正在跟踪具有给定主键值的实体,则会立即返回该实体,而无需向数据库发出请求。 否则,将为具有给定主键值的实体的数据库进行查询,如果找到该实体,则会附加到上下文并返回该实体。 如果未找到任何实体,则返回 null。 |
| FindAsync(Object[]) |
查找具有给定主键值的实体。 如果上下文正在跟踪具有给定主键值的实体,则会立即返回该实体,而无需向数据库发出请求。 否则,将为具有给定主键值的实体的数据库进行查询,如果找到该实体,则会附加到上下文并返回该实体。 如果未找到任何实体,则返回 null。 |
| GetAsyncEnumerator(CancellationToken) |
返回枚举将异步对数据库执行查询的一个 IAsyncEnumerator<T> 值。 |
| Remove(TEntity) |
开始跟踪处于状态的 Deleted 给定实体,以便调用数据库时 SaveChanges() 会将其从数据库中删除。 |
| RemoveRange(IEnumerable<TEntity>) |
开始跟踪处于状态的 Deleted 给定实体,以便在调用数据库时 SaveChanges() 将其从数据库中删除。 |
| RemoveRange(TEntity[]) |
开始跟踪处于状态的 Deleted 给定实体,以便在调用数据库时 SaveChanges() 将其从数据库中删除。 |
| Update(TEntity) |
开始跟踪给定实体和默认情况下可以使用状态 Modified 从给定实体访问的条目,但有关使用不同状态的情况,请参阅下文。 |
| UpdateRange(IEnumerable<TEntity>) |
开始跟踪默认使用 Modified 状态从给定实体访问的给定实体和条目,但有关使用不同状态的情况,请参阅下文。 |
| UpdateRange(TEntity[]) |
开始跟踪默认使用 Modified 状态从给定实体访问的给定实体和条目,但有关使用不同状态的情况,请参阅下文。 |
显式接口实现
| 名称 | 说明 |
|---|---|
| IAsyncEnumerable<TEntity>.GetAsyncEnumerator(CancellationToken) |
返回枚举将异步对数据库执行查询的一个 IAsyncEnumerator<T> 值。 |
| IAsyncEnumerableAccessor<TEntity>.AsyncEnumerable |
返回枚举将异步对数据库执行查询的一个 IAsyncEnumerable<T> 值。 |
| IEnumerable.GetEnumerator() |
返回枚举 IEnumerator 将针对数据库执行查询以从数据库加载所有实体的查询。 |
| IEnumerable<TEntity>.GetEnumerator() |
返回枚举 IEnumerator<T> 将针对数据库执行查询以从数据库加载所有实体的查询。 |
| IInfrastructure<IServiceProvider>.Instance |
获取用于解析服务的作用域 IServiceProvider 。 |
| IListSource.ContainsListCollection |
获取一个值,该值指示集合是否是 System.Collections.IList 对象的集合。
始终返回 |
| IListSource.GetList() |
尝试将数据直接绑定到 a1 DbSet<TEntity>时,数据绑定框架调用此方法。 |
| IQueryable.ElementType |
获取 IQueryable 元素类型。 |
| IQueryable.Expression |
获取 IQueryable LINQ 表达式。 |
| IQueryable.Provider |
获取 IQueryable 提供程序。 |