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

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
TContext

The type of context to be configured.

Inheritance
System.ObjectSystem.Object
DbContextOptionsBuilder<TContext>DbContextOptionsBuilder(Of TContext)
Implements

Inherited Members

Microsoft.EntityFrameworkCore.DbContextOptionsBuilder

System.Object

System.Object.ToString()System.Object.ToString()
System.Object.Equals(System.Object)System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()System.Object.GetHashCode()
System.Object.GetType()System.Object.GetType()
System.Object.MemberwiseClone()System.Object.MemberwiseClone()

Constructors

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))
Parameters
options
DbContextOptions<TContext> DbContextOptions(Of TContext)

The options to be configured.

Properties

Options Options

Gets the options being configured.

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

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<TContext> ConfigureWarnings(Action<WarningsConfigurationBuilder> warningsConfigurationBuilderAction)
Public Overridable Function ConfigureWarnings(warningsConfigurationBuilderAction As Action(Of WarningsConfigurationBuilder)) As DbContextOptionsBuilder(Of TContext)
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<TContext> EnableSensitiveDataLogging()
Public Overridable Function EnableSensitiveDataLogging As DbContextOptionsBuilder(Of TContext)
Returns

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

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

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)
Parameters
serviceProvider
System.IServiceProvider System.IServiceProvider

The System.IServiceProvider to be used.

Returns

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)
Parameters
loggerFactory
ILoggerFactory ILoggerFactory

The to be used.

Returns

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)
Parameters
memoryCache
IMemoryCache IMemoryCache

The 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<TContext> UseModel(IModel model)
Public Overridable Function UseModel(model As IModel) As DbContextOptionsBuilder(Of TContext)
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<TContext> UseQueryTrackingBehavior(QueryTrackingBehavior queryTrackingBehavior)
Public Overridable Function UseQueryTrackingBehavior(queryTrackingBehavior As QueryTrackingBehavior) As DbContextOptionsBuilder(Of TContext)
Parameters
Returns