DbContextOptionsBuilder 類別

定義

提供用於設定 DbContextOptions 的簡單 API 介面。 資料庫 (和其他延伸模組) 通常會在此物件上定義擴充方法,讓您設定資料庫連接 (和其他選項,) 用於內容。

public class DbContextOptionsBuilder : Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptionsBuilderInfrastructure
type DbContextOptionsBuilder = class
    interface IDbContextOptionsBuilderInfrastructure
Public Class DbContextOptionsBuilder
Implements IDbContextOptionsBuilderInfrastructure
繼承
DbContextOptionsBuilder
衍生
實作

備註

您可以使用 DbContextOptionsBuilder 來設定內容,方法是覆 OnConfiguring(DbContextOptionsBuilder) 寫或建立 DbContextOptions 外部,並將它傳遞至內容建構函式。

如需詳細資訊和範例 ,請參閱使用 DbCoNtextOptions

建構函式

DbContextOptionsBuilder()

初始化 未設定任何選項之 DbContextOptionsBuilder 類別的新實例。

DbContextOptionsBuilder(DbContextOptions)

初始化 類別的新實例, DbContextOptionsBuilder 以進一步設定指定的 DbContextOptions

屬性

IsConfigured

取得值,指出是否已設定任何選項。

Options

取得正在設定的選項。

方法

AddInterceptors(IEnumerable<IInterceptor>)

IInterceptor 實例加入至在內容上註冊的實例。

AddInterceptors(IInterceptor[])

IInterceptor 實例加入至在內容上註冊的實例。

ConfigureLoggingCacheTime(TimeSpan)

設定 EF Core 在特定高效能路徑中快取記錄設定的時間長度。 這會使 EF Core 略過可能耗費成本的記錄檢查,但表示執行時間記錄變更 (例如,註冊新的 DiagnosticListener 可能不會立即納入考慮) 。

ConfigureWarnings(Action<WarningsConfigurationBuilder>)

設定 Entity Framework 所產生的警告執行時間行為。 您可以為每個警告類型設定預設行為和行為。

ConfigureWarnings(Action<WarningsConfigurationBuilder>)

設定 Entity Framework 所產生的警告執行時間行為。 您可以為每個警告類型設定預設行為和行為。

EnableDetailedErrors(Boolean)

在處理存放區查詢結果期間發生的資料值例外狀況時,啟用詳細的錯誤。 這類錯誤最常因為實體屬性設定錯誤而發生。 例如,如果屬性設定為類型為 'int',但存放區中的基礎資料實際上是類型 'string',則在處理資料值期間,會在執行時間產生例外狀況。 啟用此選項並遇到資料錯誤時,產生的例外狀況將會包含產生錯誤之特定實體屬性的詳細資料。

EnableSensitiveDataLogging()

可讓應用程式資料包含在例外狀況訊息、記錄等中。這可以包含指派給實體實例屬性的值、傳送至資料庫的命令參數值,以及其他這類資料。 只有根據此資料的敏感度,才應該啟用此旗標。

EnableSensitiveDataLogging(Boolean)

可讓應用程式資料包含在例外狀況訊息、記錄等中。這可以包含指派給實體實例屬性的值、傳送至資料庫的命令參數值,以及其他這類資料。 只有根據此資料的敏感度,才應該啟用此旗標。

EnableServiceProviderCaching(Boolean)

啟用或停用內部服務提供者的快取。 停用快取可能會大幅影響效能,而且只應用於建置許多服務提供者以進行測試隔離的測試案例。

EnableThreadSafetyChecks(Boolean)

停用並行偵測,它會偵測實例的多個錯誤並行使用 DbContext 案例,並導致擲回信息例外狀況。 這會提供一些效能改善,但如果 DbContext 同時使用 實例,行為將會未定義,而且程式可能會以無法預期的方式失敗。

LogTo(Action<String>, Func<EventId,LogLevel,Boolean>, Nullable<DbContextLoggerOptions>)

記錄由提供的自訂篩選委派篩選的事件。 篩選準則應該會傳回 true 來記錄訊息,或傳回 false 以將它篩選掉記錄檔。

LogTo(Action<String>, IEnumerable<EventId>, LogLevel, Nullable<DbContextLoggerOptions>)

使用提供的動作記錄指定的事件。 例如,使用 optionsBuilder.LogTo(Console.WriteLine, new[] { CoreEventId.ContextInitialized }) 將事件記錄 ContextInitialized 到主控台。

LogTo(Action<String>, IEnumerable<String>, LogLevel, Nullable<DbContextLoggerOptions>)

使用提供的動作,記錄指定類別中的所有事件。 例如,使用 optionsBuilder.LogTo(Console.WriteLine, new[] { DbLoggerCategory.Infrastructure.Name }) 來記錄類別中的所有 DbLoggerCategory.Infrastructure 事件。

LogTo(Action<String>, LogLevel, Nullable<DbContextLoggerOptions>)

使用提供的動作記錄。 例如,使用 optionsBuilder.LogTo(Console.WriteLine) 來登入主控台。

LogTo(Func<EventId,LogLevel,Boolean>, Action<EventData>)

將事件記錄至自訂篩選委派篩選的自訂記錄器委派。 篩選準則應該會傳回 true 來記錄訊息,或傳回 false 以將它篩選掉記錄檔。

ReplaceService<TService,TCurrentImplementation,TNewImplementation>()

以不同的實作取代服務合約特定實作的內部 Entity Framework 實作。

ReplaceService<TService,TImplementation>()

以不同的實作取代服務合約的所有內部 Entity Framework 實作。

UseApplicationServiceProvider(IServiceProvider)

IServiceProvider設定要從中取得應用程式服務的 。 使用 'AddDbCoNtext' 或 'AddDbCoNtextPool' 時,這會自動完成,因此很少需要呼叫這個方法。

UseInternalServiceProvider(IServiceProvider)

IServiceProvider設定內容應該從中解析其所有服務的 。 如果未指定,EF 將會建立和管理服務提供者。

UseLoggerFactory(ILoggerFactory)

ILoggerFactory設定 ,用來建立 ILogger 實例以供此內容完成的記錄。

UseMemoryCache(IMemoryCache)

IMemoryCache設定要用於此內容之查詢快取的 。

UseModel(IModel)

設定要用於內容的模型。 如果已設定模型, OnModelCreating(ModelBuilder) 則不會執行。

UseQueryTrackingBehavior(QueryTrackingBehavior)

設定針對內容執行的 LINQ 查詢追蹤行為。 停用變更追蹤對於唯讀案例很有用,因為它可避免為每個實體實例設定變更追蹤的額外負荷。 如果您想要操作實體實例,並使用 將那些變更保存到資料庫 SaveChanges() ,則不應該停用變更追蹤。

UseRootApplicationServiceProvider()

從範圍應用程式服務提供者解析根 IServiceProvider 目錄。 根提供者可用來從單一內部服務取得單一應用程式服務。

UseRootApplicationServiceProvider(IServiceProvider)

設定單一應用程式服務可從單一內部服務取得的根 IServiceProvider 目錄。

明確介面實作

IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension<TExtension>(TExtension)

將指定的延伸模組新增至選項。 如果已經存在相同類型的現有延伸模組,則會加以取代。

擴充方法

UseCosmos(DbContextOptionsBuilder, String, TokenCredential, String, Action<CosmosDbContextOptionsBuilder>)

設定內容以連線到 Azure Cosmos 資料庫。

UseCosmos(DbContextOptionsBuilder, String, String, Action<CosmosDbContextOptionsBuilder>)

設定內容以連線到 Azure Cosmos 資料庫。

UseCosmos(DbContextOptionsBuilder, String, String, String, Action<CosmosDbContextOptionsBuilder>)

設定內容以連線到 Azure Cosmos 資料庫。

UseInMemoryDatabase(DbContextOptionsBuilder, Action<InMemoryDbContextOptionsBuilder>)
已淘汰.
已淘汰.

設定內容以連線到舊版共用記憶體內部資料庫。 這個方法已過時。 請改用 UseInMemoryDatabase(DbContextOptionsBuilder, String, Action<InMemoryDbContextOptionsBuilder>)

UseInMemoryDatabase(DbContextOptionsBuilder, String, InMemoryDatabaseRoot, Action<InMemoryDbContextOptionsBuilder>)

設定內容以連線到具名記憶體內部資料庫。 記憶體內部資料庫會在使用相同名稱的任何位置共用,但僅適用于指定的服務提供者。

UseInMemoryDatabase(DbContextOptionsBuilder, String, Action<InMemoryDbContextOptionsBuilder>)

設定內容以連線到具名記憶體內部資料庫。 記憶體內部資料庫會在使用相同名稱的任何位置共用,但僅適用于指定的服務提供者。 若要跨服務提供者使用相同的記憶體內部資料庫,請呼叫 UseInMemoryDatabase(DbContextOptionsBuilder, String, InMemoryDatabaseRoot, Action<InMemoryDbContextOptionsBuilder>) 將共用 InMemoryDatabaseRoot 傳遞給資料庫的根目錄。

UseChangeTrackingProxies(DbContextOptionsBuilder, Boolean, Boolean)

開啟變更追蹤 Proxy 的建立。

UseLazyLoadingProxies(DbContextOptionsBuilder, Action<LazyLoadingProxiesOptionsBuilder>)

開啟延遲載入 Proxy 的建立。

UseLazyLoadingProxies(DbContextOptionsBuilder, Boolean)

開啟延遲載入 Proxy 的建立。

UseSqlite(DbContextOptionsBuilder, Action<SqliteDbContextOptionsBuilder>)

設定內容以連線到 SQLite 資料庫,但不一開始設定任何 DbConnection 或連接字串。

UseSqlite(DbContextOptionsBuilder, DbConnection, Action<SqliteDbContextOptionsBuilder>)

設定內容以連線到 SQLite 資料庫。

UseSqlite(DbContextOptionsBuilder, DbConnection, Boolean, Action<SqliteDbContextOptionsBuilder>)

設定內容以連線到 SQLite 資料庫。

UseSqlite(DbContextOptionsBuilder, String, Action<SqliteDbContextOptionsBuilder>)

設定內容以連線到 SQLite 資料庫。

UseSqlServer(DbContextOptionsBuilder, Action<SqlServerDbContextOptionsBuilder>)

設定內容以連線到 Microsoft SQL Server 資料庫,但不一開始設定任何 DbConnection 或連接字串。

UseSqlServer(DbContextOptionsBuilder, DbConnection, Action<SqlServerDbContextOptionsBuilder>)

設定內容以連線到 Microsoft SQL Server 資料庫。

UseSqlServer(DbContextOptionsBuilder, DbConnection, Boolean, Action<SqlServerDbContextOptionsBuilder>)

設定內容以連線到 Microsoft SQL Server 資料庫。

UseSqlServer(DbContextOptionsBuilder, String, Action<SqlServerDbContextOptionsBuilder>)

設定內容以連線到 Microsoft SQL Server 資料庫。

適用於