我正在尝试将IDbCommandInterceptor集成到我的项目中。我正在使用EntityFramework 6.0版。我创建了这样的拦截器类:
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);
}
}
}
当我尝试使用代码配置添加此拦截器类时,它的工作正常。但我想在文章Interception中使用配置设置添加此拦截器。我的配置文件如下所示:
<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>
但它给出的错误是:
无法识别的元素&#39;拦截器&#39;
我在app配置文件中设置了实体配置:
<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" />
注意:我的数据访问层位于单独的类库中。 EDMX文件存在于数据项目中。表示层引用了数据层。实体框架存在于项目中且具有相同的版本。
答案 0 :(得分:2)
我通过更新Entity Framework的版本解决了这个问题。
旧版本:6.0.2 更新版本:6.1.3
答案 1 :(得分:2)
简答:EF拦截器只能配置从6.1开始的配置文件,参考
EF Config settings from Microsoft
长答案: 此外,如果您去检查C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ Xml \ Schemas目录并尝试打开EntityFrameworkConfig_6_0_0.xsd,您会注意到拦截器未声明为子元素:
EntityFrameworkConfig_6_0_0.xsd的内容(不包含 拦截器标签)
<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>
EntityFrameworkConfig_6_1_0.xsd的内容(包含 拦截器标签)
<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>