다음을 통해 공유


DbContextOptionsBuilder Class

Definition

Provides a simple API surface for configuring DbContextOptions. Databases (and other extensions) typically define extension methods on this object that allow you to configure the database connection (and other options) to be used for a context.

public class DbContextOptionsBuilder : Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptionsBuilderInfrastructure
type DbContextOptionsBuilder = class
    interface IDbContextOptionsBuilderInfrastructure
Public Class DbContextOptionsBuilder
Implements IDbContextOptionsBuilderInfrastructure
Inheritance
DbContextOptionsBuilder
Derived
Implements

Remarks

You can use DbContextOptionsBuilder to configure a context by overriding OnConfiguring(DbContextOptionsBuilder) or creating a DbContextOptions externally and passing it to the context constructor.

See Using DbContextOptions for more information and examples.

Constructors

DbContextOptionsBuilder()

Initializes a new instance of the DbContextOptionsBuilder class with no options set.

DbContextOptionsBuilder(DbContextOptions)

Initializes a new instance of the DbContextOptionsBuilder class to further configure a given DbContextOptions.

Properties

IsConfigured

Gets a value indicating whether any options have been configured.

Options

Gets the options being configured.

Methods

AddInterceptors(IEnumerable<IInterceptor>)

Adds IInterceptor instances to those registered on the context.

AddInterceptors(IInterceptor[])

Adds IInterceptor instances to those registered on the context.

ConfigureLoggingCacheTime(TimeSpan)

Configures how long EF Core will cache logging configuration in certain high-performance paths. This makes EF Core skip potentially costly logging checks, but means that runtime logging changes (e.g. registering a new DiagnosticListener may not be taken into account right away).

ConfigureWarnings(Action<WarningsConfigurationBuilder>)

Configures the runtime behavior of warnings generated by Entity Framework. You can set a default behavior and behaviors for each warning type.

ConfigureWarnings(Action<WarningsConfigurationBuilder>)

Configures the runtime behavior of warnings generated by Entity Framework. You can set a default behavior and behaviors for each warning type.

EnableDetailedErrors(Boolean)

Enables detailed errors when handling of data value exceptions that occur during processing of store query results. Such errors most often occur due to misconfiguration of entity properties. E.g. If a property is configured to be of type 'int', but the underlying data in the store is actually of type 'string', then an exception will be generated at runtime during processing of the data value. When this option is enabled and a data error is encountered, the generated exception will include details of the specific entity property that generated the error.

EnableSensitiveDataLogging()

Enables application data to be included in exception messages, logging, etc. This can include the values assigned to properties of your entity instances, parameter values for commands being sent to the database, and other such data. You should only enable this flag if you have the appropriate security measures in place based on the sensitivity of this data.

EnableSensitiveDataLogging(Boolean)

Enables application data to be included in exception messages, logging, etc. This can include the values assigned to properties of your entity instances, parameter values for commands being sent to the database, and other such data. You should only enable this flag if you have the appropriate security measures in place based on the sensitivity of this data.

EnableServiceProviderCaching(Boolean)

Enables or disables caching of internal service providers. Disabling caching can massively impact performance and should only be used in testing scenarios that build many service providers for test isolation.

EnableThreadSafetyChecks(Boolean)

Disables concurrency detection, which detects many cases of erroneous concurrent usage of a DbContext instance and causes an informative exception to be thrown. This provides a minor performance improvement, but if a DbContext instance is used concurrently, the behavior will be undefined and the program may fail in unpredictable ways.

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

Logs events filtered by a supplied custom filter delegate. The filter should return true to log a message, or false to filter it out of the log.

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

Logs the specified events using the supplied action. For example, use optionsBuilder.LogTo(Console.WriteLine, new[] { CoreEventId.ContextInitialized }) to log the ContextInitialized event to the console.

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

Logs all events in the specified categories using the supplied action. For example, use optionsBuilder.LogTo(Console.WriteLine, new[] { DbLoggerCategory.Infrastructure.Name }) to log all events in the DbLoggerCategory.Infrastructure category.

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

Logs using the supplied action. For example, use optionsBuilder.LogTo(Console.WriteLine) to log to the console.

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

Logs events to a custom logger delegate filtered by a custom filter delegate. The filter should return true to log a message, or false to filter it out of the log.

ReplaceService<TService,TCurrentImplementation,TNewImplementation>()

Replaces the internal Entity Framework implementation of a specific implementation of a service contract with a different implementation.

ReplaceService<TService,TImplementation>()

Replaces all internal Entity Framework implementations of a service contract with a different implementation.

UseApplicationServiceProvider(IServiceProvider)

Sets the IServiceProvider from which application services will be obtained. This is done automatically when using 'AddDbContext' or 'AddDbContextPool', so it is rare that this method needs to be called.

UseAsyncSeeding(Func<DbContext,Boolean,CancellationToken,Task>)

Configures the seed method to run after EnsureCreatedAsync(CancellationToken) is called or after migrations are applied asynchronously. It will be invoked even if no changes to the store were performed.

UseInternalServiceProvider(IServiceProvider)

Sets the IServiceProvider that the context should resolve all of its services from. EF will create and manage a service provider if none is specified.

UseLoggerFactory(ILoggerFactory)

Sets the ILoggerFactory that will be used to create ILogger instances for logging done by this context.

UseMemoryCache(IMemoryCache)

Sets the IMemoryCache to be used for query caching by this context.

UseModel(IModel)

Sets the model to be used for the context. If the model is set, then OnModelCreating(ModelBuilder) will not be run.

UseQueryTrackingBehavior(QueryTrackingBehavior)

Sets the tracking behavior for LINQ queries run against the context. Disabling change tracking is useful for read-only scenarios because it avoids the overhead of setting up change tracking for each entity instance. You should not disable change tracking if you want to manipulate entity instances and persist those changes to the database using SaveChanges().

UseRootApplicationServiceProvider()

Resolves the root IServiceProvider from the scoped application service provider. The root provider can be used to obtain singleton application services from singleton internal services.

UseRootApplicationServiceProvider(IServiceProvider)

Sets the root IServiceProvider from which singleton application services can be obtained from singleton internal services.

UseSeeding(Action<DbContext,Boolean>)

Configures the seed method to run after EnsureCreated() is called or after migrations are applied. It will be invoked even if no changes to the store were performed.

Explicit Interface Implementations

IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension<TExtension>(TExtension)

Adds the given extension to the options. If an existing extension of the same type already exists, it will be replaced.

Extension Methods

UseCosmos(DbContextOptionsBuilder, Action<CosmosDbContextOptionsBuilder>)

Configures the context to connect to an Azure Cosmos database. The connection details need to be specified in a separate call.

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

Configures the context to connect to an Azure Cosmos database.

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

Configures the context to connect to an Azure Cosmos database.

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

Configures the context to connect to an Azure Cosmos database.

UseInMemoryDatabase(DbContextOptionsBuilder, Action<InMemoryDbContextOptionsBuilder>)
Obsolete.
Obsolete.

Configures the context to connect to the legacy shared in-memory database. This method is obsolete. Use UseInMemoryDatabase(DbContextOptionsBuilder, String, Action<InMemoryDbContextOptionsBuilder>) instead.

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

Configures the context to connect to a named in-memory database. The in-memory database is shared anywhere the same name is used, but only for a given service provider.

UseInMemoryDatabase(DbContextOptionsBuilder, String, Action<InMemoryDbContextOptionsBuilder>)

Configures the context to connect to a named in-memory database. The in-memory database is shared anywhere the same name is used, but only for a given service provider. To use the same in-memory database across service providers, call UseInMemoryDatabase(DbContextOptionsBuilder, String, InMemoryDatabaseRoot, Action<InMemoryDbContextOptionsBuilder>) passing a shared InMemoryDatabaseRoot on which to root the database.

UseChangeTrackingProxies(DbContextOptionsBuilder, Boolean, Boolean)

Turns on the creation of change tracking proxies.

UseLazyLoadingProxies(DbContextOptionsBuilder, Action<LazyLoadingProxiesOptionsBuilder>)

Turns on the creation of lazy loading proxies.

UseLazyLoadingProxies(DbContextOptionsBuilder, Boolean)

Turns on the creation of lazy loading proxies.

UseSqlite(DbContextOptionsBuilder, Action<SqliteDbContextOptionsBuilder>)

Configures the context to connect to a SQLite database, but without initially setting any DbConnection or connection string.

UseSqlite(DbContextOptionsBuilder, DbConnection, Action<SqliteDbContextOptionsBuilder>)

Configures the context to connect to a SQLite database.

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

Configures the context to connect to a SQLite database.

UseSqlite(DbContextOptionsBuilder, String, Action<SqliteDbContextOptionsBuilder>)

Configures the context to connect to a SQLite database.

ConfigureSqlEngine(DbContextOptionsBuilder, Action<SqlEngineDbContextOptionsBuilder>)

Configures the context to connect to any of SQL Server, Azure SQL, Azure Synapse databases, but without initially setting any DbConnection or connection string.

UseAzureSql(DbContextOptionsBuilder, Action<AzureSqlDbContextOptionsBuilder>)

Configures the context to connect to a Azure SQL database, but without initially setting any DbConnection or connection string.

UseAzureSql(DbContextOptionsBuilder, DbConnection, Action<AzureSqlDbContextOptionsBuilder>)

Configures the context to connect to a Azure SQL database.

UseAzureSql(DbContextOptionsBuilder, DbConnection, Boolean, Action<AzureSqlDbContextOptionsBuilder>)

Configures the context to connect to a Azure SQL database.

UseAzureSql(DbContextOptionsBuilder, String, Action<AzureSqlDbContextOptionsBuilder>)

Configures the context to connect to a Azure SQL database.

UseAzureSynapse(DbContextOptionsBuilder, Action<AzureSynapseDbContextOptionsBuilder>)

Configures the context to connect to a Azure Synapse database, but without initially setting any DbConnection or connection string.

UseAzureSynapse(DbContextOptionsBuilder, DbConnection, Action<AzureSynapseDbContextOptionsBuilder>)

Configures the context to connect to a Azure Synapse database.

UseAzureSynapse(DbContextOptionsBuilder, DbConnection, Boolean, Action<AzureSynapseDbContextOptionsBuilder>)

Configures the context to connect to a Azure Synapse database.

UseAzureSynapse(DbContextOptionsBuilder, String, Action<AzureSynapseDbContextOptionsBuilder>)

Configures the context to connect to a Azure Synapse database.

UseSqlServer(DbContextOptionsBuilder, Action<SqlServerDbContextOptionsBuilder>)

Configures the context to connect to a SQL Server database, but without initially setting any DbConnection or connection string.

UseSqlServer(DbContextOptionsBuilder, DbConnection, Action<SqlServerDbContextOptionsBuilder>)

Configures the context to connect to a SQL Server database.

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

Configures the context to connect to a SQL Server database.

UseSqlServer(DbContextOptionsBuilder, String, Action<SqlServerDbContextOptionsBuilder>)

Configures the context to connect to a SQL Server database.

Applies to