Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将调试信息注入实体框架查询_C#_Sql_Sql Server_Entity Framework_Instrumentation - Fatal编程技术网

C# 将调试信息注入实体框架查询

C# 将调试信息注入实体框架查询,c#,sql,sql-server,entity-framework,instrumentation,C#,Sql,Sql Server,Entity Framework,Instrumentation,我们在商店中使用了Dapper和EF,当出现问题时,Dapper被证明对调试SQL server中的查询非常有帮助。我们不只是提交原始SQL,而是创建了一个精简装饰器,它还添加了一些上下文信息(源代码)作为SQL注释,如 /* Foo.Bar.GetOrders() */ SELECT * FROM Order WHERE orderId > 123 这使我们的DBA和开发人员能够在数据库调用出错或导致性能下降时快速找到问题的根源(我们每天有数十万个数据库调用,因此一个错误的查询可能会造

我们在商店中使用了Dapper和EF,当出现问题时,Dapper被证明对调试SQL server中的查询非常有帮助。我们不只是提交原始SQL,而是创建了一个精简装饰器,它还添加了一些上下文信息(源代码)作为SQL注释,如

/* Foo.Bar.GetOrders() */ SELECT * FROM Order WHERE orderId > 123
这使我们的DBA和开发人员能够在数据库调用出错或导致性能下降时快速找到问题的根源(我们每天有数十万个数据库调用,因此一个错误的查询可能会造成相当大的损害)

我们也希望与EF一起这样做。它不必是SQL注释,而是某种钩子,以便提供随调用提交的元信息。你知道这是否可能吗

谢谢你的建议


Philipp

结果表明,使用EF 6,这变得非常容易。所需要的只是一个IDbCommandInterceptor的实现,它允许我用一个自定义(SQL)注释来扩充提交的SQL。该注释将出现在数据库日志中,从而启用DBA端的调试/跟踪

public class DebugCommentInterceptor : IDbCommandInterceptor
{
    public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
    {
        command.CommandText = "/* TRACING INFORMATION GOES HERE */ " + command.CommandText;
    }

    public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        command.CommandText = "/* TRACING INFORMATION GOES HERE */ " + command.CommandText;
    }

    public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
    }

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

    public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
    }

    public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
    }
}

如果使用EF Core,则可以使用查询标记

var nearestFriends =
  (from f in context.Friends.TagWith("This is my spatial query!")
  orderby f.Location.Distance(myLocation) descending
  select f).Take(5).ToList();
有一个新的linq
.TagWith(“”)
,它将生成Sql查询,其中包含:

-- This is my spatial query!

我刚刚发现EF 6具有更好的拦截能力()。我没有时间玩它,但可能在不久的将来会有时间在这里报告。承认已经过了几年了…你还记得你是如何得到来电者的信息插入这里的吗?这似乎比拦截部分更复杂。(当你调用他们的公共API时,Dapper似乎捕捉到了这些信息,而EF不允许这样做。)恐怕我不知道,我目前甚至还没有在.NET上工作。希望其他人能加入进来——祝你好运!
-- This is my spatial query!