DbConfiguration.SetContextFactory Method

 

Namespace:   System.Data.Entity
Assembly:  EntityFramework (in EntityFramework.dll)

NameDescription
System_CAPS_protmethodSetContextFactory(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.

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


DbConfiguration.SetContextFactory Method (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<DbContext> factory
)

Parameters

contextType
Type: System.Type

The context type for which the factory should be used.

factory
Type: System.Func<DbContext>

The delegate to use to create context instances.

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.


DbConfiguration.SetContextFactory<TContext> Method (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 : DbContext

Parameters

factory
Type: System.Func<TContext>

The delegate to use to create context instances.

Type Parameters

TContext

The context type for which the factory should be used.

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.