DbConfiguration.SetContextFactory DbConfiguration.SetContextFactory DbConfiguration.SetContextFactory Method

Definition

Overloads

SetContextFactory(Type, Func<DbContext>) SetContextFactory(Type, Func<DbContext>) SetContextFactory(Type, Func<DbContext>)

Call this method from the constructor of a class derived from DbConfiguration to set a factory to allow DbContextInfo to create instances of a context that does not have a public, parameterless constructor.

SetContextFactory<TContext>(Func<TContext>) SetContextFactory<TContext>(Func<TContext>) SetContextFactory<TContext>(Func<TContext>)

Call this method from the constructor of a class derived from DbConfiguration to set a factory to allow DbContextInfo to create instances of a context that does not have a public, parameterless constructor.

SetContextFactory(Type, Func<DbContext>) SetContextFactory(Type, Func<DbContext>) SetContextFactory(Type, Func<DbContext>)

Call this method from the constructor of a class derived from DbConfiguration to set a factory to allow DbContextInfo to create instances of a context that does not have a public, parameterless constructor.

protected internal void SetContextFactory (Type contextType, Func<System.Data.Entity.DbContext> factory);
member this.SetContextFactory : Type * Func<System.Data.Entity.DbContext> -> unit
Protected Friend Sub SetContextFactory (contextType As Type, factory As Func(Of DbContext))

Parameters

contextType
Type Type Type

The context type for which the factory should be used.

factory
Func<DbContext>

The delegate to use to create context instances.

Remarks

This is typically needed to allow design-time tools like Migrations or scaffolding code to use contexts that do not have public, parameterless constructors. This method is provided as a convenient and discoverable way to add configuration to the Entity Framework. Internally it works in the same way as using AddDependencyResolver to add an appropriate resolver for Func<TResult> with the context Type as the key. This means that, if desired, the same functionality can be achieved using a custom resolver or a resolver backed by an Inversion-of-Control container.

SetContextFactory<TContext>(Func<TContext>) SetContextFactory<TContext>(Func<TContext>) SetContextFactory<TContext>(Func<TContext>)

Call this method from the constructor of a class derived from DbConfiguration to set a factory to allow DbContextInfo to create instances of a context that does not have a public, parameterless constructor.

protected internal void SetContextFactory<TContext> (Func<TContext> factory) where TContext : System.Data.Entity.DbContext;
member this.SetContextFactory : Func<'Context (requires 'Context :> System.Data.Entity.DbContext)> -> unit (requires 'Context :> System.Data.Entity.DbContext)
Protected Friend Sub SetContextFactory(Of TContext As DbContext) (factory As Func(Of TContext))

Type Parameters

TContext

The context type for which the factory should be used.

Parameters

factory
Func<TContext>

The delegate to use to create context instances.

Remarks

This is typically needed to allow design-time tools like Migrations or scaffolding code to use contexts that do not have public, parameterless constructors. This method is provided as a convenient and discoverable way to add configuration to the Entity Framework. Internally it works in the same way as using AddDependencyResolver to add an appropriate resolver for Func<TResult> with the context Type as the key. This means that, if desired, the same functionality can be achieved using a custom resolver or a resolver backed by an Inversion-of-Control container.

Applies to