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

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

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

Constructors

DbContextOptionsBuilder() DbContextOptionsBuilder() DbContextOptionsBuilder()

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

DbContextOptionsBuilder(DbContextOptions) DbContextOptionsBuilder(DbContextOptions) DbContextOptionsBuilder(DbContextOptions)

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

Properties

IsConfigured IsConfigured IsConfigured

Gets a value indicating whether any options have been configured.

This can be useful when you have overridden OnConfiguring(DbContextOptionsBuilder) to configure the context, but in some cases you also externally provide options via the context constructor. This property can be used to determine if the options have already been set, and skip some or all of the logic in OnConfiguring(DbContextOptionsBuilder).

Options Options Options

Gets the options being configured.

Methods

ConfigureWarnings(Action<WarningsConfigurationBuilder>) ConfigureWarnings(Action<WarningsConfigurationBuilder>) 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.

Note that changing this configuration can cause EF to build a new internal service provider, which may cause issues with performance. Generally it is expected that no more than one or two different configurations will be used for a given application.

Note that if the application is setting the internal service provider through a call to UseInternalServiceProvider(IServiceProvider), then this option must configured the same way for all uses of that service provider. Consider instead not calling UseInternalServiceProvider(IServiceProvider) so that EF will manage the service providers and can create new instances as required.

ConfigureWarnings(Action<WarningsConfigurationBuilder>) ConfigureWarnings(Action<WarningsConfigurationBuilder>) 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.

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

Note that if the application is setting the internal service provider through a call to UseInternalServiceProvider(IServiceProvider), then this option must configured the same way for all uses of that service provider. Consider instead not calling UseInternalServiceProvider(IServiceProvider) so that EF will manage the service providers and can create new instances as required.

Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

ReplaceService<TService,TImplementation>() ReplaceService<TService,TImplementation>() ReplaceService<TService,TImplementation>()

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

This method can only be used when EF is building and managing its internal service provider. If the service provider is being built externally and passed to UseInternalServiceProvider(IServiceProvider), then replacement services should be configured on that service provider before it is passed to EF.

The replacement service gets the same scope as the EF service that it is replacing.

ToString() ToString() ToString()

Returns a string that represents the current object.

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

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

The service provider must contain all the services required by Entity Framework (and the database being used). The Entity Framework services can be registered using an extension method on IServiceCollection. For example, the Microsoft SQL Server provider includes an AddEntityFrameworkSqlServer() method to add the required services.

If the IServiceProvider has a DbContextOptions or DbContextOptions<TContext> registered, then this will be used as the options for this context instance.

UseLoggerFactory(ILoggerFactory) UseLoggerFactory(ILoggerFactory) UseLoggerFactory(ILoggerFactory)

Sets the ILoggerFactory that will be used to create ILogger instances for logging done by this context. It is never necessary to call this method since EF can obtain or create a logger factory automatically.

There is no need to call this method when using one of the 'AddDbContext' methods. 'AddDbContext' will ensure that the ILoggerFactory used by EF is obtained from the application service provider.

Note that changing the logger factory can cause EF to build a new internal service provider, which may cause issues with performance. Generally it is expected that no more than one or two different instances will be used for a given application.

This method cannot be used if the application is setting the internal service provider through a call to UseInternalServiceProvider(IServiceProvider). In this case, the ILoggerFactory should be configured directly in that service provider.

UseMemoryCache(IMemoryCache) UseMemoryCache(IMemoryCache) UseMemoryCache(IMemoryCache)

Sets the IMemoryCache to be used for query caching by this context. It is never necessary to call this method since EF can obtain or create a memory cache automatically.

There is no need to call this method when using one of the 'AddDbContext' methods. 'AddDbContext' will ensure that the IMemoryCache used by EF is obtained from the application service provider.

Note that changing the memory cache can cause EF to build a new internal service provider, which may cause issues with performance. Generally it is expected that no more than one or two different instances will be used for a given application.

This method cannot be used if the application is setting the internal service provider through a call to UseInternalServiceProvider(IServiceProvider). In this case, the IMemoryCache should be configured directly in that service provider.

UseModel(IModel) UseModel(IModel) 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) UseQueryTrackingBehavior(QueryTrackingBehavior) 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().

This method sets the default behavior for all contexts created with these options, but you can override this behavior for a context instance using QueryTrackingBehavior or on individual queries using the AsNoTracking<TEntity>(IQueryable<TEntity>) and AsTracking<TEntity>(IQueryable<TEntity>) methods.

The default value is TrackAll. This means the change tracker will keep track of changes for all entities that are returned from a LINQ query.

Explicit Interface Implementations

IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension<TExtension>(TExtension) IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension<TExtension>(TExtension) 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.

This method is intended for use by extension methods to configure the context. It is not intended to be used in application code.

Extension Methods

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

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

UseInMemoryDatabase(DbContextOptionsBuilder, String, InMemoryDatabaseRoot, Action<InMemoryDbContextOptionsBuilder>) UseInMemoryDatabase(DbContextOptionsBuilder, String, InMemoryDatabaseRoot, Action<InMemoryDbContextOptionsBuilder>) 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>) UseInMemoryDatabase(DbContextOptionsBuilder, String, Action<InMemoryDbContextOptionsBuilder>) 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.

AsConcreteMetadataType<TInterface,TConcrete>(TInterface, String) AsConcreteMetadataType<TInterface,TConcrete>(TInterface, String) AsConcreteMetadataType<TInterface,TConcrete>(TInterface, String)

This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

UseLazyLoadingProxies(DbContextOptionsBuilder, Boolean) UseLazyLoadingProxies(DbContextOptionsBuilder, Boolean) UseLazyLoadingProxies(DbContextOptionsBuilder, Boolean)

Turns on the creation of lazy-loading proxies.

Note that this requires appropriate services to be available in the EF internal service provider. Normally this will happen automatically, but if the application is controlling the service provider, then a call to AddEntityFrameworkProxies(IServiceCollection) may be needed.

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

Configures the context to connect to a SQLite database.

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

Configures the context to connect to a SQLite database.

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

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

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

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

Applies to