Code-based Configuration in Entity Framework 6

Entity Framework 6 has introduced code-based configuration. Now, you can configure Entity Framework related settings using the code which has been previously configured in the <entityframework> section of the app.config. However, app.config takes precedence over code-based configuration. In other words, if a configuration option is set in both the code and app.config, then the setting in the app.config is used.

Let's see how to implement code-based configuration using Entity Framework 6.

First of all, you need to create a new class that derives the DbConfiguration (System.Data.Entity.DbConfiguration) class :

public class FE6CodeConfig : DbConfiguration
{
    public FE6CodeConfig()
    {
        //define configuration here
    }
}

Now, you can set the codeConfigurationType attribute in the app.config as shown below:

<entityFramework codeConfigurationType="EF6DBFirstTutorials.FE6CodeConfig, EF6DBFirstTutorials">
</entityFramework>

Or you can use the DbConfigurationType attribute on the context class to set the code-based configuration class:

async query output

Note: EF does not support having multiple configuration classes used in the same AppDomain. If you use this attribute to set different configuration classes for the two contexts, then an exception will be thrown.

Now, you can use different methods of DbConfiguration in the constructor as shown below:

async query output

Let's see how to apply different settings using code-based configuration as well as the app.config.

Configuring the Default Connection Factory

Use the SetDefaultConnectionFactory() method to configure the default connection factory such as SqlConnectionFactory for SQL Server, as shown below.

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

The default connection factory is configured in app.config as shown below.

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>

Setting Database Provider

Use the SetProviderServices() method to configure the database provider, as shown below.

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

The database provider can be configured in app.config in EF 6, as shown below.

<entityFramework>
    <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>

Setting Database Initializers

You can set database initializers (for Code-First only) using code-based configuration as shown below:

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

The same thing as above can be configured in app.config as shown below.

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