Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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# 是否可以在EF中执行存储过程之前获取查询字符串?_C#_Sql_Entity Framework_Stored Procedures - Fatal编程技术网

C# 是否可以在EF中执行存储过程之前获取查询字符串?

C# 是否可以在EF中执行存储过程之前获取查询字符串?,c#,sql,entity-framework,stored-procedures,C#,Sql,Entity Framework,Stored Procedures,是否可以在执行之前获取实体框架为存储过程生成的查询? 例如,呼叫sp context.Test(1) 拿到绳子 exec[dbo]。测试1 但是在执行之前,如果您使用的是Entity Framework 6,那么您可以使用查询拦截器在SQL生成和SQL执行之间插入代码。它是通过实现IDB拦截器来实现的。您可以附加到以下“事件”: namespace System.Data.Entity.Infrastructure.Interception { 公共接口IDbCommandInterceptor

是否可以在执行之前获取实体框架为存储过程生成的查询? 例如,呼叫sp

context.Test(1)

拿到绳子

exec[dbo]。测试1


但是在执行之前,如果您使用的是Entity Framework 6,那么您可以使用查询拦截器在SQL生成和SQL执行之间插入代码。它是通过实现
IDB拦截器来实现的。您可以附加到以下“事件”:

namespace System.Data.Entity.Infrastructure.Interception
{
公共接口IDbCommandInterceptor:IDbCommandInterceptor
{
void非查询执行(DbCommand命令,DbCommandInterceptionContext interceptionContext);
void NonQueryExecuted(DbCommand命令,DbCommandInterceptionContext interceptionContext);
void ReaderExecuting(DbCommand命令、DbCommandInterceptionContext interceptionContext);
void ReaderExecuted(DbCommand命令,DbCommandInterceptionContext interceptionContext);
void ScalarExecuting(DbCommand命令、DbCommandInterceptionContext interceptionContext);
void scalarecuted(DbCommand命令,DbCommandInterceptionContext interceptionContext);
}
}
您可以编写实现上述接口的自定义拦截器,然后通过调用以下命令将其添加到EF中:

DbInterception.Add(new())


这里还有其他关于查看实体框架生成的SQL的建议:,但这取决于您是只查看SQL还是在执行SQL之前执行一些操作。

您使用哪个EF版本?@MarcinZablocki 6ok。谢谢,但是可以让查询字符串修改它然后执行吗?那么,我是否可以在默认情况下停止执行
namespace System.Data.Entity.Infrastructure.Interception
{
  public interface IDbCommandInterceptor : IDbInterceptor
  {
    void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext);

    void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext);

    void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext);

    void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext);

    void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext);

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