Reputation: 1339
I am trying to integrate IDbCommandInterceptor to my project. I am using EntityFramework version 6.0. I created interceptor class like this:
namespace MyApp.Data
{
public class EFCommandInterceptor : IDbCommandInterceptor
{
public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
LogInfo("NonQueryExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}
public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
LogInfo("NonQueryExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}
public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
{
LogInfo("ReaderExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}
public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
{
LogInfo("ReaderExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}
public void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
LogInfo("ScalarExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}
public void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
LogInfo("ScalarExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}
private void LogInfo(string command, string commandText)
{
Console.WriteLine("Intercepted on: {0} :- {1} ", command, commandText);
}
}
}
When I try to add this interceptor class using code configuration then its working fine. But I want to add this interceptor using config setting as mention in article Interception. My config file looks like this:
<entityFramework>
<interceptors>
<interceptor type="MyApp.Data.EFCommandInterceptor, MyApp.Data"></interceptor>
</interceptors>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
But it is giving error that:
Unrecognized element 'interceptors'
I have entity config setting in app config file:
<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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
Note: My data access layer is in separate class library. EDMX file is present in Data project. Presentation layer has reference to Data layer. Entity framework is present in both the project and has same version.
Upvotes: 1
Views: 2144
Reputation: 303
Short Answer: EF Interceptors can only be configured with Config file starting from 6.1, Refer
EF Config settings from Microsoft
Long Answer: Moreover, If you go and check C:\Program Files (x86)\Microsoft Visual Studio 12.0\Xml\Schemas directory and try to open EntityFrameworkConfig_6_0_0.xsd you will notice that interceptors is not declared as child element:
Content of EntityFrameworkConfig_6_0_0.xsd (Does not contain Interceptors tag)
<xs:element name="entityFramework">
<xs:complexType>
<xs:all>
<xs:element name="defaultConnectionFactory" type="ElementWithTypeAndParameters_Type" minOccurs="0" maxOccurs="1" />
<xs:element name="providers" type="ProviderList_Type" minOccurs="0" maxOccurs="1" />
<xs:element name="contexts" type="ContextList_Type" minOccurs="0" maxOccurs="1" />
</xs:all>
<xs:attribute name="codeConfigurationType" type="NonEmptyString_Type" use="optional" />
<xs:anyAttribute namespace="http://schemas.microsoft.com/XML-Document-Transform" processContents="strict"/>
</xs:complexType>
</xs:element>
Content of EntityFrameworkConfig_6_1_0.xsd (Does contain Interceptors tag)
<xs:element name="entityFramework">
<xs:complexType>
<xs:all>
<xs:element name="defaultConnectionFactory" type="ElementWithTypeAndParameters_Type" minOccurs="0" maxOccurs="1" />
<xs:element name="providers" type="ProviderList_Type" minOccurs="0" maxOccurs="1" />
<xs:element name="contexts" type="ContextList_Type" minOccurs="0" maxOccurs="1" />
<xs:element name="interceptors" type="InterceptorList_Type" minOccurs="0" maxOccurs="1" />
</xs:all>
<xs:attribute name="codeConfigurationType" type="NonEmptyString_Type" use="optional" />
<xs:anyAttribute namespace="http://schemas.microsoft.com/XML-Document-Transform" processContents="strict"/>
</xs:complexType>
</xs:element>
Upvotes: 3
Reputation: 1339
I have resolved the issue by just updating the version of Entity Framework.
Older Version: 6.0.2 Updated Version: 6.1.3
Upvotes: 2