Sometimes, We want to check the original sql statements. creating a commandInterceptor is a good way to do this.

Add a class named MyCommandInterceptor

class MyCommandInterceptor: DbCommandInterceptor
{
    public override void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        base.NonQueryExecuted(command, interceptionContext);
        Console.WriteLine(command.CommandText);
    }

    public override void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
    {
        base.ReaderExecuted(command, interceptionContext);
        Console.WriteLine(command.CommandText);
    }

    public override void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        base.ScalarExecuted(command, interceptionContext);
        Console.WriteLine(command.CommandText);
    }
}

The namespace System.Data.Entity.Infrastructure.Interception is needed. Then Add DbInterception.Add(new MyCommandInterceptor()); in your constructor of DbContext class:

public class MyDb:DbContext
{
    public MyDb():base("name=TestDb")
    {
        DbInterception.Add(new MyCommandInterceptor());
    }

    public IDbSet<User> Users { get; set; }
}

It's all.

相关文章:

  • 2021-11-12
  • 2021-12-22
  • 2022-01-19
  • 2022-02-18
  • 2021-11-06
  • 2021-07-06
  • 2021-12-15
  • 2021-07-15
猜你喜欢
  • 2021-11-23
  • 2021-05-20
  • 2021-09-18
  • 2021-10-09
  • 2021-12-26
  • 2022-01-03
  • 2022-01-06
相关资源
相似解决方案