Entity Framework为我们提供了很大的方便,但有时候,我们想看看EF生成的Sql语句到底是怎样的,一种方式是我们可以启用Sql Server Profer工具。今天介绍另外一种方式,非常简单,可以监听EF执行的每条Sql语句,而且也可以自定义在执行语句前,执行语句完成后的动作。通过这种方式也可以非常方便的去监听系统的运行日志。

           完成这个功能,需要引入二个第三方的组件,分别是Clutch.dll和Clutch.Diagnostics.EntityFramework.dll(网上可以下载到)。引入之后,只需要实现IDbTracingListener接口:

class TestListener : IDbTracingListener
{
    public void CommandExecuted(DbTracingContext context)
    {
        Debug.WriteLine(context.Command.CommandText);
    }
 
    public void CommandExecuting(DbTracingContext context)
    {
        //throw new NotImplementedException();
    }
 
    public void CommandFailed(DbTracingContext context)
    {
        //throw new NotImplementedException();
    }
 
    public void CommandFinished(DbTracingContext context)
    {
        //throw new NotImplementedException();
    }
 
    public void ReaderFinished(DbTracingContext context)
    {
        //throw new NotImplementedException();
    }
}

相关文章:

  • 2022-12-23
  • 2021-07-06
  • 2022-02-27
  • 2021-11-21
  • 2021-08-28
  • 2021-10-28
  • 2021-07-10
猜你喜欢
  • 2022-12-23
  • 2022-01-09
  • 2021-11-27
  • 2021-11-03
  • 2021-11-19
  • 2022-12-23
  • 2022-02-24
相关资源
相似解决方案