[英]IDbCommandInterceptor never be called
I have created an application base on Entity Framework. 我已经基于实体框架创建了一个应用程序。 To separate reading and writing of the database, I am trying to use IDbCommandInterceptor.
为了分开读取和写入数据库,我尝试使用IDbCommandInterceptor。 As for my thought, IDbCommandInterceptor will catch the access from EF to database and I could change the connection into another one.
按照我的想法,IDbCommandInterceptor将捕获从EF到数据库的访问,我可以将连接更改为另一个。 Connection is normal,I could get the result from database normally.However, The only embarassed thing is that, my custom DbCommandInterceptor class has even not been called!
连接正常,我可以从数据库中正常获得结果。但是,唯一令人尴尬的是,我的自定义DbCommandInterceptor类甚至都没有被调用!
My custom Interceptor: 我的自定义拦截器:
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)
{
//I add a break point here, but it has never been called;
}
}
The code to call Interceptor: 调用拦截器的代码:
var cmdInterceptor = new EFCommandInterceptor();
System.Data.Entity.Infrastructure.Interception.DbInterception.Add(cmdInterceptor);
using (var context = new CSMDBContainer())
{
var task = context.T_TASK.FirstOrDefault();
context.SaveChanges();
}
System.Data.Entity.Infrastructure.Interception.DbInterception.Remove(cmdInterceptor);
Entity Framework section in configuration file: 配置文件中的“实体框架”部分:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
Any suggestion will be appreciated! 任何建议将不胜感激!
Finally, I resolved this issue. 最后,我解决了这个问题。 The route is that, my Context class generated by EF inherits ObjectContext .
路线是,由EF生成的Context类继承了ObjectContext 。
After I rebuilt the edmx file and made the Context class inherit DbContext , the issue has gone. 在重建edmx文件并使Context类继承DbContext之后 ,问题就消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.