DbContextOptionsBuilder<TContext> Class

Definition

Provides a simple API surface for configuring DbContextOptions<TContext>. 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<TContext> : Microsoft.EntityFrameworkCore.DbContextOptionsBuilder where TContext : DbContext
type DbContextOptionsBuilder<'Context (requires 'Context :> DbContext)> = class
    inherit DbContextOptionsBuilder
Public Class DbContextOptionsBuilder(Of TContext)
Inherits DbContextOptionsBuilder

Type Parameters

TContext

The type of context to be configured.

Inheritance
DbContextOptionsBuilder<TContext>

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<TContext>()

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

DbContextOptionsBuilder<TContext>(DbContextOptions<TContext>)

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

Properties

IsConfigured

Gets a value indicating whether any options have been configured.

(Inherited from DbContextOptionsBuilder)
Options

Gets the options being configured.

Methods

AddInterceptors(IEnumerable<IInterceptor>)

Adds IInterceptor instances to those registered on the context.

AddInterceptors(IEnumerable<IInterceptor>)

Adds IInterceptor instances to those registered on the context.

(Inherited from DbContextOptionsBuilder)
AddInterceptors(IInterceptor[])

Adds IInterceptor instances to those registered on the context.

AddInterceptors(IInterceptor[])

Adds IInterceptor instances to those registered on the context.

(Inherited from DbContextOptionsBuilder)
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).

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

(Inherited from DbContextOptionsBuilder)
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.

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.

(Inherited from DbContextOptionsBuilder)
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.

(Inherited from DbContextOptionsBuilder)
EnableDetailedErrors(Boolean)

Enables detailed errors when handling 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.

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.

(Inherited from DbContextOptionsBuilder)
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()

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.

(Inherited from DbContextOptionsBuilder)
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.

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.

(Inherited from DbContextOptionsBuilder)
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.

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.

(Inherited from DbContextOptionsBuilder)
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.

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.

(Inherited from DbContextOptionsBuilder)
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>, 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.

(Inherited from DbContextOptionsBuilder)
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<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.

(Inherited from DbContextOptionsBuilder)
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>, 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.

(Inherited from DbContextOptionsBuilder)
LogTo(Action<String>, LogLevel, Nullable<DbContextLoggerOptions>)

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

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

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

(Inherited from DbContextOptionsBuilder)
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.

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.

(Inherited from DbContextOptionsBuilder)
ReplaceService<TService,TCurrentImplementation,TNewImplementation>()

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

ReplaceService<TService,TCurrentImplementation,TNewImplementation>()

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

(Inherited from DbContextOptionsBuilder)
ReplaceService<TService,TImplementation>()

Replaces all internal Entity Framework implementations 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.

(Inherited from DbContextOptionsBuilder)
UseApplicationServiceProvider(IServiceProvider)

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

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.

(Inherited from DbContextOptionsBuilder)
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().

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

(Inherited from DbContextOptionsBuilder)
UseRootApplicationServiceProvider()

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

UseRootApplicationServiceProvider()

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

(Inherited from DbContextOptionsBuilder)
UseRootApplicationServiceProvider(IServiceProvider)

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

UseRootApplicationServiceProvider(IServiceProvider)

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

(Inherited from DbContextOptionsBuilder)

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.

(Inherited from DbContextOptionsBuilder)

Extension Methods

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.

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

Configures the context to connect to an Azure Cosmos database.

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

Configures the context to connect to an Azure Cosmos database.

UseCosmos<TContext>(DbContextOptionsBuilder<TContext>, 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.

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

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

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

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

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

Configures the context to connect to an 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<TContext>(DbContextOptionsBuilder<TContext>, 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.

UseChangeTrackingProxies<TContext>(DbContextOptionsBuilder<TContext>, 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.

UseLazyLoadingProxies<TContext>(DbContextOptionsBuilder<TContext>, Action<LazyLoadingProxiesOptionsBuilder>)

Turns on the creation of lazy loading proxies.

UseLazyLoadingProxies<TContext>(DbContextOptionsBuilder<TContext>, 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.

UseSqlite<TContext>(DbContextOptionsBuilder<TContext>, Action<SqliteDbContextOptionsBuilder>)

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

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

Configures the context to connect to a SQLite database.

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

Configures the context to connect to a SQLite database.

UseSqlite<TContext>(DbContextOptionsBuilder<TContext>, String, Action<SqliteDbContextOptionsBuilder>)

Configures the context to connect to a SQLite database.

UseSqlServer(DbContextOptionsBuilder, Action<SqlServerDbContextOptionsBuilder>)

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

UseSqlServer(DbContextOptionsBuilder, DbConnection, Action<SqlServerDbContextOptionsBuilder>)

Configures the context to connect to a Microsoft SQL Server database.

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

Configures the context to connect to a Microsoft SQL Server database.

UseSqlServer(DbContextOptionsBuilder, String, Action<SqlServerDbContextOptionsBuilder>)

Configures the context to connect to a Microsoft SQL Server database.

UseSqlServer<TContext>(DbContextOptionsBuilder<TContext>, Action<SqlServerDbContextOptionsBuilder>)

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

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

Configures the context to connect to a Microsoft SQL Server database.

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

Configures the context to connect to a Microsoft SQL Server database.

UseSqlServer<TContext>(DbContextOptionsBuilder<TContext>, String, Action<SqlServerDbContextOptionsBuilder>)

Configures the context to connect to a Microsoft SQL Server database.

Applies to