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.

Syntax

Declaration

public class DbContextOptionsBuilder<TContext> : DbContextOptionsBuilder, IDbContextOptionsBuilderInfrastructure where TContext : DbContextPublic Class DbContextOptionsBuilder(Of TContext As DbContext)
    Inherits DbContextOptionsBuilder
    Implements IDbContextOptionsBuilderInfrastructure

Type Parameters

TContext The type of context to be configured.

Inheritance Hierarchy

Inherited Members

IsConfigured,
IsConfigured,
IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension<TExtension>(TExtension),
Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension(Of TExtension)(TExtension),
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<TContext> class with no options set.

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

Properties summary

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

Sets the used for logging information from the context.

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

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.

Constructors

  • DbContextOptionsBuilder()
    DbContextOptionsBuilder()

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

    public DbContextOptionsBuilder()Public Sub New
  • DbContextOptionsBuilder(DbContextOptions<TContext>)
    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

Properties

  • Options
    Options

    Gets the options being configured.

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

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

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

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

    Returns

  • 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

  • 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

  • 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

  • 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

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

    Parameters

    Returns

Details

Assembly

Microsoft.EntityFrameworkCore.dll