DbContextOptionsBuilder<TContext> DbContextOptionsBuilder(Of TContext) Class


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.

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<TContext> : DbContextOptionsBuilder, IDbContextOptionsBuilderInfrastructure where TContext : DbContext
Public Class DbContextOptionsBuilder(Of TContext As DbContext)
    Inherits DbContextOptionsBuilder
    Implements IDbContextOptionsBuilderInfrastructure
Type Parameters

The type of context to be configured.

DbContextOptionsBuilder<TContext>DbContextOptionsBuilder(Of TContext)


DbContextOptionsBuilder() DbContextOptionsBuilder()

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

public DbContextOptionsBuilder()
Public Sub New

DbContextOptionsBuilder(DbContextOptions) DbContextOptionsBuilder(DbContextOptions(Of TContext))

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

public DbContextOptionsBuilder(DbContextOptions<TContext> options)
Public Sub New(options As DbContextOptions(Of TContext))
DbContextOptions<TContext> DbContextOptions(Of TContext)

The options to be configured.


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

(Inherited from DbContextOptionsBuilder)

Options Options

Gets the options being configured.

public virtual DbContextOptions<TContext> Options { get; }
Public Overridable ReadOnly Property Options As DbContextOptions(Of TContext)
DbContextOptions<TContext> DbContextOptions(Of TContext)


ConfigureWarnings(Action) ConfigureWarnings(Action(Of WarningsConfigurationBuilder))

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

public virtual DbContextOptionsBuilder<TContext> ConfigureWarnings(Action<WarningsConfigurationBuilder> warningsConfigurationBuilderAction)
Public Overridable Function ConfigureWarnings(warningsConfigurationBuilderAction As Action(Of WarningsConfigurationBuilder)) As DbContextOptionsBuilder(Of TContext)
System.Action<WarningsConfigurationBuilder> System.Action(Of WarningsConfigurationBuilder)

An action to configure the warning behavior.


The same builder instance so that multiple calls can be chained.


    optionsBuilder.ConfigureWarnings(warnings => 
                .Log(CoreEventId.IncludeIgnoredWarning, CoreEventId.ModelValidationWarning)

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.

public virtual DbContextOptionsBuilder<TContext> EnableSensitiveDataLogging()
Public Overridable Function EnableSensitiveDataLogging As DbContextOptionsBuilder(Of TContext)

The same builder instance so that multiple calls can be chained.

Equals(Object) Equals(Object)

Inherited from System.Object

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

Inherited from System.Object

GetHashCode() GetHashCode()

Inherited from System.Object

GetType() GetType()

Inherited from System.Object

IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension(TExtension) Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension(Of TExtension)(TExtension)

Inherited from DbContextOptionsBuilder

MemberwiseClone() MemberwiseClone()

Inherited from System.Object

ReferenceEquals(Object, Object) ReferenceEquals(Object, Object)

Inherited from System.Object

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

public virtual DbContextOptionsBuilder<TContext> ReplaceService<TService, TImplementation>()where TImplementation : TService
Public Overridable Function ReplaceService(Of TService, TImplementation As TService) As DbContextOptionsBuilder(Of TContext)
Type Parameters

The type (usually an interface) that defines the contract of the service to replace.


The new implementation type for the service.


The same builder instance so that multiple calls can be chained.

ToString() ToString()

Inherited from System.Object

UseInternalServiceProvider(IServiceProvider) UseInternalServiceProvider(IServiceProvider)

Sets the System.IServiceProvider that the context will resolve its internal services from. If none is specified, then Entity Framework will maintain its own internal System.IServiceProvider. By default, we recommend allowing Entity Framework to create and maintain its own System.IServiceProvider for internal services.

public virtual DbContextOptionsBuilder<TContext> UseInternalServiceProvider(IServiceProvider serviceProvider)
Public Overridable Function UseInternalServiceProvider(serviceProvider As IServiceProvider) As DbContextOptionsBuilder(Of TContext)
System.IServiceProvider System.IServiceProvider

The System.IServiceProvider to be used.


The same builder instance so that multiple calls can be chained.

UseLoggerFactory(ILoggerFactory) UseLoggerFactory(ILoggerFactory)

Sets the used for logging information from the context.

public virtual DbContextOptionsBuilder<TContext> UseLoggerFactory(ILoggerFactory loggerFactory)
Public Overridable Function UseLoggerFactory(loggerFactory As ILoggerFactory) As DbContextOptionsBuilder(Of TContext)
ILoggerFactory ILoggerFactory

The to be used.


The same builder instance so that multiple calls can be chained.

UseMemoryCache(IMemoryCache) UseMemoryCache(IMemoryCache)

Sets the used to cache information such as query translations. If none is specified, then Entity Framework will maintain its own internal .

public virtual DbContextOptionsBuilder<TContext> UseMemoryCache(IMemoryCache memoryCache)
Public Overridable Function UseMemoryCache(memoryCache As IMemoryCache) As DbContextOptionsBuilder(Of TContext)
IMemoryCache IMemoryCache

The to be used.


The same builder instance so that multiple calls can be chained.

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.

public virtual DbContextOptionsBuilder<TContext> UseModel(IModel model)
Public Overridable Function UseModel(model As IModel) As DbContextOptionsBuilder(Of TContext)
IModel IModel

The model to be used.


The same builder instance so that multiple calls can be chained.

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.

public virtual DbContextOptionsBuilder<TContext> UseQueryTrackingBehavior(QueryTrackingBehavior queryTrackingBehavior)
Public Overridable Function UseQueryTrackingBehavior(queryTrackingBehavior As QueryTrackingBehavior) As DbContextOptionsBuilder(Of TContext)