Skip to main content

This content relates to a pre-release version of Entity Framework (EF6)

For more information, see Future Versions


 

Entity Framework Code-Based Configuration

Configuration for an Entity Framework application can be specified in a config file (app.config/web.config) or through code. The latter is known as code-based configuration.

Configuration in a config file is described in a separate article. The config file takes precedence over code-based configuration. In other words, if a configuration option is set in both code and in the config file, then the setting in the config file is used.

 

Using DbConfiguration

Code-based configuration in EF6 and above is achieved by creating a subclass of System.Data.Entity.Config.DbConfiguration. The following guidelines should be followed when subclassing DbConfiguration:

  • Create only one DbConfiguration class for your application. This class specifies app-domain wide settings.
  • Place your DbConfiguration class in the same assembly as your DbContext class. (See the Moving DbConfiguration section if you want to change this.)
  • Give your DbConfiguration class a public parameterless constructor.
  • Set configuration options by calling protected DbConfiguration methods from within this constructor.

Following these guidelines allows EF to discover and use your configuration automatically by both tooling that needs to access your model and when your application is run.

 

Example

A class derived from DbConfiguration might look like this:

public class MyConfiguration : DbConfiguration
{
    public MyConfiguration()
    {
        SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));
        AddProvider("My.New.Provider", new MyProviderServices());
    }
}

This class sets up EF to use Local DB for databases that are created by convention and also registers a new EF provider.

 

Moving DbConfiguration

There are cases where it is not possible to place your DbConfiguration class in the same assembly as your DbContext class. For example, you may have two DbContext classes each in different assemblies. There are two options for handling this.

The first option is to use the config file to specify the DbConfiguration instance to use. To do this, set the codeConfigurationType attribute of the entityFramework section. For example:

<entityFramework codeConfigurationType="MyNamespace.MyDbConfiguration, MyAssembly">
    ...Your EF config...
</entityFramework>

The value of codeConfigurationType must be the assembly and namespace qualified name of your DbConfiguration class.

The second option is to place DbConfigurationTypeAttribute on your context class. For example:

[DbConfigurationType(typeof(MyDbConfiguration))]
public class MyContextContext : DbContext
{
}

The value passed to the attribute can either be your DbConfiguration type or the assembly and namespace qualified type name string. For example:

[DbConfigurationType("MyNamespace.MyDbConfiguration, MyAssembly")]
public class MyContextContext : DbContext
{
}

 

Setting DbConfiguration explicitly

There are some situations where configuration may be needed before any DbContext type has been used. Examples of this include:

  • Using DbModelBuilder to build a model without a context
  • Using some other framework/utility code that utilizes a DbContext where that context is used before your application context is used

In such situations EF is unable to discover the configuration automatically and you must instead do one of the following:

  • Set the DbConfiguration type in the config file, as described in the Moving DbConfiguration section above
  • Call the static DbConfiguration.SetConfiguration method during application startup

Microsoft is conducting an online survey to understand your opinion of the MSDN Web site. If you choose to participate, the online survey will be presented to you when you leave the MSDN Web site.

Would you like to participate?