Configuration File Settings

Description Article
Table of Contents
Summary

In EF 4.3 was introduced the custom <entityFramework> section to handle the new settings that where previously included on the <appSettings> of the application configuration file.

Description

The <entityFramework> section is automatically added to the application configuration file of a project when the EntityFramework NuGet package is installed:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework"
       type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>
</configuration>

 

 

Operations
Configuration of database initializers per context
How to do it
<entityFramework>
  <contexts>
    <context type="EF6DBFirstTutorials.SchoolDBEntities, EF6DBFirstTutorials"  >
      <databaseInitializer   type="EF.Demo.CustomDBInitializer, EF.Demo">
      </databaseInitializer>
    </context>
  </contexts>
</entityFramework>
Configuration of the default connection factory

For example, the SqlCeConnectionFactory, that is included in Entity Framework, requires to supply a provider invariant name to the constructor. The provider invariant name identifies the version of SQL Compact to use.

How to do it

The following configuration will cause contexts to use SQL Compact version 4.0 by default:

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="System.Data.SqlServerCe.4.0" />
    </parameters>
  </defaultConnectionFactory>
</entityFramework>
Configuration of database providers (EF6.1 Onwards)

Providers are registered by including a provider element under the providers child section.

There are two required attributes for a provider entry:

1) invariantName: identifies the core ADO.NET provider that this EF provider targets
2) type: is the assembly qualified type name of the EF provider implementation

Remarks

An assembly qualified name is the namespace qualified name, followed by a comma, then the assembly that the type resides in.

Optionally also specify the assembly version, culture and public key token.

How to do it
<entityFramework>
  <providers>
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  </providers>
</entityFramework>
Configuration of interceptors for logging database operations (EF6.1 Onwards)

DatabaseLogger supports logging to a file by supplying the file name as a constructor parameter.

How to do it
<entityFramework>
  <interceptors>
    <interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework">
      <parameters>
        <parameter value="C:\Temp\LogOutput.txt"/>
      </parameters>
    </interceptor>
  </interceptors>
</entityFramework>

By default this will cause the log file to be overwritten with a new file each time the application starts. To instead append to the log file if it already exists use something like:

<entityFramework>
  <interceptors>
    <interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework">
      <parameters>
        <parameter value="C:\Temp\LogOutput.txt"/>
        <parameter value="true" type="System.Boolean"/>
      </parameters>
    </interceptor>
  </interceptors>
</entityFramework>
Configuration of other interceptors

Configuration of other interceptors allow to run additional logic when EF performs certain operations, such as executing database queries, opening connections, etc.