EF 查询可以为空吗?

Could EF query be empty?

我在我的项目中实现了 IDbCommandInterceptor 接口。我想在它的每个函数(ReaderExecuted、NonQueryExecuted,...)中进行记录。问题是 "Is there a situation, when DbCommand command is null?" 在网络上一些使用 command?.CommandText 的例子中,我想确定这段代码不会是 "dog-nail".

public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
            Logger(command, interceptionContext);
        }

我不这么认为,至少通常不这么认为。 DbCommand class contains its own (non-static) execution methods, and DbConnection does not,因此您需要一个 DbCommand 实例来正常触发 (I)DbCommandInterceptor 处理程序。也就是说,IDbCommandInterceptor 只是一个接口,任何引用拦截器的人都可以使用空参数手动调用它的任何 public 方法。这取决于您期望人们使用您的代码时行为良好。