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 : IDbContextOptionsBuilderInfrastructure
Public Class DbContextOptionsBuilder
    Implements IDbContextOptionsBuilderInfrastructure
Inheritance
System.ObjectSystem.Object
DbContextOptionsBuilderDbContextOptionsBuilder
Implements

Constructors

DbContextOptionsBuilder() DbContextOptionsBuilder()

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

public DbContextOptionsBuilder()
Public Sub New

DbContextOptionsBuilder(DbContextOptions) DbContextOptionsBuilder(DbContextOptions)

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

public DbContextOptionsBuilder(DbContextOptions options)
Public Sub New(options As DbContextOptions)
Parameters
options
DbContextOptions DbContextOptions

The options to be configured.

Properties

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

public virtual bool IsConfigured { get; }
Public Overridable ReadOnly Property IsConfigured As Boolean
Value
System.Boolean System.Boolean

Options Options

Gets the options being configured.

public virtual DbContextOptions Options { get; }
Public Overridable ReadOnly Property Options As DbContextOptions

Methods

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 ConfigureWarnings(Action<WarningsConfigurationBuilder> warningsConfigurationBuilderAction)
Public Overridable Function ConfigureWarnings(warningsConfigurationBuilderAction As Action(Of WarningsConfigurationBuilder)) As DbContextOptionsBuilder
Parameters
warningsConfigurationBuilderAction
System.Action<WarningsConfigurationBuilder> System.Action(Of WarningsConfigurationBuilder)

An action to configure the warning behavior.

Returns

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

Examples

    optionsBuilder.ConfigureWarnings(warnings => 
        warnings.Default(WarningBehavior.Ignore)
                .Log(CoreEventId.IncludeIgnoredWarning, CoreEventId.ModelValidationWarning)
                .Throw(RelationalEventId.QueryClientEvaluationWarning))

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 EnableSensitiveDataLogging()
Public Overridable Function EnableSensitiveDataLogging As DbContextOptionsBuilder
Returns

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

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 ReplaceService<TService, TImplementation>()where TImplementation : TService
Public Overridable Function ReplaceService(Of TService, TImplementation As TService) As DbContextOptionsBuilder
Type Parameters
TService

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

TImplementation

The new implementation type for the service.

Returns

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 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 the AddEntityFramework(IServiceCollection) method. Most databases also provide an extension method on to register the services required. For example, the Microsoft SQL Server provider includes an AddEntityFrameworkSqlServer() method to add the required services.

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

public virtual DbContextOptionsBuilder UseInternalServiceProvider(IServiceProvider serviceProvider)
Public Overridable Function UseInternalServiceProvider(serviceProvider As IServiceProvider) As DbContextOptionsBuilder
Parameters
serviceProvider
System.IServiceProvider System.IServiceProvider

The service provider to be used.

Returns

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

UseLoggerFactory(ILoggerFactory) UseLoggerFactory(ILoggerFactory)

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

public virtual DbContextOptionsBuilder UseLoggerFactory(ILoggerFactory loggerFactory)
Public Overridable Function UseLoggerFactory(loggerFactory As ILoggerFactory) As DbContextOptionsBuilder
Parameters
loggerFactory
ILoggerFactory ILoggerFactory

The logger factory to be used.

Returns

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

UseMemoryCache(IMemoryCache) UseMemoryCache(IMemoryCache)

Sets the to be used for query caching by this context. EF will create and manage a memory cache if none is specified.

public virtual DbContextOptionsBuilder UseMemoryCache(IMemoryCache memoryCache)
Public Overridable Function UseMemoryCache(memoryCache As IMemoryCache) As DbContextOptionsBuilder
Parameters
memoryCache
IMemoryCache IMemoryCache

The memory cache to be used.

Returns

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 UseModel(IModel model)
Public Overridable Function UseModel(model As IModel) As DbContextOptionsBuilder
Parameters
model
IModel IModel

The model to be used.

Returns

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 UseQueryTrackingBehavior(QueryTrackingBehavior queryTrackingBehavior)
Public Overridable Function UseQueryTrackingBehavior(queryTrackingBehavior As QueryTrackingBehavior) As DbContextOptionsBuilder
Parameters
Returns

Explicit Interface Implementations

IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension(TExtension) Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension(Of 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.

void IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension<TExtension>(TExtension extension)where TExtension : class, IDbContextOptionsExtension
Sub Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension(Of TExtension As {Class, IDbContextOptionsExtension})(extension As TExtension) Implements IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension
Type Parameters
TExtension

The type of extension to be added.

Parameters
extension
TExtension TExtension

The extension to be added.