DbContextOptionsBuilder DbContextOptionsBuilder Class

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.

Syntax

Declaration

public class DbContextOptionsBuilder : IDbContextOptionsBuilderInfrastructurePublic Class DbContextOptionsBuilder
    Implements IDbContextOptionsBuilderInfrastructure

Inheritance Hierarchy

  • System.Object
    System.Object
  • DbContextOptionsBuilder
    DbContextOptionsBuilder

Inherited Members

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

Constructors summary

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

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

Properties summary

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

Gets the options being configured.

Methods summary

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

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.

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.

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.

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

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

Sets the model to be used for the context. If the model is set, then OnModelCreating(ModelBuilder) will not be run.

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 summary

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.

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

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

    Property Value

    • System.Boolean
      System.Boolean
  • Options
    Options

    Gets the options being configured.

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

    Property Value

Methods

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

    Returns

    Example

        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

  • ReplaceService<TService, TImplementation>()
    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 : TServicePublic Overridable Function ReplaceService(Of TService, TImplementation As TService) As DbContextOptionsBuilder

    Returns

  • 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

  • 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

  • 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

  • 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

    Returns

  • 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>(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, IDbContextOptionsExtensionSub Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension(Of TExtension As {Class, IDbContextOptionsExtension})(extension As TExtension) Implements IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension

    Parameters

    • extension
      TExtension
      TExtension

      The extension to be added.

Details

Assembly

Microsoft.EntityFrameworkCore.dll