Code-based Configuration Settings

Description Article
Table of Contents
Summary

In EF6 was introduced code based configuration.

Now its possible to configure entity framework related settings using code which had been previously configured in the <entityframework> section of the “app.config”/”web.config” configuration files. However, the last takes precedence over code-based configuration.

Description

How It Works

This class specifies app-domain wide settings, and therefore only one DbConfiguration class can be created per application.
How To Do It

A class derived from the DbConfiguration class is expected in the same assembly as a class derived from DbContext to define Entity Framework configuration for an application,a nd

  1. The DbConfiguration class must have a public parameterless constructor.
  2. Configuration is set by calling protected methods and setting protected properties of this class in the constructor of the derived type.

Remarks

When it is not possible to place the DbConfiguration class in the same assembly as the DbContext class (e.g. two distinct DbContext classes exists, each in different assemblies),
two distinct options exist:

Option 1: Set codeConfigurationType attribute in the configuration file as shown below:

    <entityFramework codeConfigurationType="EF.Demo.CodeBasedConfiguration, EF.Demo">
    </entityFramework>

Option 2: Use the DbConfigurationType attribute on the context class to set the code-based configuration class. The value passed to the attribute can either be the DbConfiguration type or the assembly and namespace qualified type name string:

[DbConfigurationType("EF.Demo.CodeBasedConfiguration, EF.Demo")]
//[DbConfigurationType(typeof(CodeBasedConfiguration))]
public class MyContextContext : DbContext
{
}

A class derived from DbConfiguration might look like this:

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.SqlServer; 

namespace MyNamespace
{
    public class MyConfiguration : DbConfiguration
    {
        public MyConfiguration()
        {
            SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
            SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));
        }
    }
}

This class sets up EF to use the SQL Azure execution strategy – to automatically retry failed database operations – and to use Local DB for databases that are created by convention from Code First.

 

Operations
Set default connection factory
How to do it

Code-based configuration:

public class CodeBasedConfiguration: DbConfiguration
{
  public CodeBasedConfiguration()
  {
    this.SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.SqlConnectionFactory());
  }
}

Configuration file:

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
Set Database Provider
How to do it

Code-based configuration:

public class CodeBasedConfiguration: DbConfiguration
{
  public CodeBasedConfiguration()
  {
    this.SetProviderServices("System.Data.SqlClient",
                        System.Data.Entity.SqlServer.SqlProviderServices.Instance);
  }
}

Configuration file:

<entityFramework>
  <providers>
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  </providers>
</entityFramework>
Set Database Initializers
How to do it

Code-based configuration:

public class CodeBasedConfiguration: DbConfiguration
{
  public CodeBasedConfiguration()
  {
    this.SetDatabaseInitializer<BreakAwayContext>(new CustomDBInitializer<BreakAwayContext>());
  }
}

Configuration file:

<entityFramework>
  <contexts>
    <context type="EF6DBFirstTutorials.SchoolDBEntities, EF6DBFirstTutorials"  >
      <databaseInitializer   type="EF.Demo.CustomDBInitializer, EF.Demo">
      </databaseInitializer>
    </context>
  </contexts>
</entityFramework>
References

[1] Refer to this article for description of class DbConfiguration
[2] Refer to this article for a first overview of code-based configuration