IDbCommandInterceptor查询监听

IDbCommandInterceptor query monitoring

我在 EF6 中创建了一个查询拦截器:

public class QueryCommandInterceptor : IDbCommandInterceptor
{
    private readonly Stopwatch _stopWatch = new Stopwatch();

    public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
            _stopWatch.Restart();
        }

        public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
            _stopWatch.Stop();
        }
}

是否有可能两个Executing 方法在其相应的Executed 方法完成之前开始?

你的意思是,这个 IDbCommandInterceptor 线程安全吗?

Is IDbCommandInterceptor in EntityFramework 6 thread safe

答案是否定的