DbSet<TEntity> 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
DbSet<TEntity> A 可用來查詢並儲存 的TEntity實例。
對 的 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>
備註
對 的 DbSet<TEntity> LINQ 查詢結果將包含從資料庫回傳的結果,且可能不反映上下文中未持久化至資料庫的變更。 例如,結果不會包含新加入的實體,且可能仍包含標記為刪除的實體。
根據所使用的資料庫,LINQ 查詢 DbSet<TEntity> 的某些部分可能會在記憶體中評估,而非轉譯成資料庫查詢。
DbSet<TEntity> 物件通常是從 DbSet<TEntity> 導出 DbContext 的屬性或 Set<TEntity>() 方法中取得的。
Entity Framework Core 不支援在同一 DbContext 執行個體上執行多個平行作業。 這包括平行執行非同步查詢,以及明確同時使用多個執行緒。 因此,務必立即等待非同步呼叫,或使用獨立的 DbContext 實例來執行平行執行的操作。 更多資訊與範例請參見 「避免 DbContext 執行緒問題 」。
更多資訊與範例請參閱 DbContext 生命週期、配置與初始化、使用 EF Core 查詢資料,以及 變更追蹤 。
建構函式
| 名稱 | Description |
|---|---|
| DbSet<TEntity>() |
DbSet<TEntity> A 可用來查詢並儲存 的 |
屬性
| 名稱 | Description |
|---|---|
| EntityType |
IEntityType與此集合相關的元資料。 |
| Local |
獲得 LocalView<TEntity> 代表本集合中所有新增、未變更及修改實體的局部視圖。 |
方法
| 名稱 | Description |
|---|---|
| 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 該狀態,但下文說明使用不同狀態的情況。 |
明確介面實作
| 名稱 | Description |
|---|---|
| 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() |
當資料綁定框架嘗試直接 DbSet<TEntity>對資料綁定時,會呼叫此方法。 |
| IQueryable.ElementType |
獲得IQueryable元素類型。 |
| IQueryable.Expression |
能得到IQueryable的LINQ表達式。 |
| IQueryable.Provider |
取得IQueryable的提供者。 |