【问题标题】:How to use IDbCommandInterceptor to modify SQL when CommandType is StoredProcedureCommandType为StoredProcedure时如何使用IDbCommandInterceptor修改SQL
【发布时间】:2016-11-11 19:37:07
【问题描述】:

我正在使用 Entity Framework 6,并希望在对数据进行任何修改之前设置 CONTEXT_INFO,以便我的审核触发器可以记录哪个用户进行了修改。似乎这应该可以使用IDbCommandInterceptor 更改将要执行的CommandText,类似于下面的代码。但是如果DbCommand.CommandType == CommandType.StoredProcedure 则它不喜欢这样,因为它期望CommandText 将是只是 过程名称,并且如果我在它前面加上我的额外SQL 就会引发错误。

public class SetContextInterceptor : IDbCommandInterceptor
{

    public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        command.CommandText = GetSqlForSetContextInfo() + command.CommandText;
    }

     // ... other interface methods similar

    private string GetSqlForSetContextInfo()
    {
        var currentUsername = Thread.CurrentPrincipal != null && Thread.CurrentPrincipal.Identity != null
            ? Thread.CurrentPrincipal.Identity.Name
            : null;

        if (String.IsNullOrEmpty(currentUsername))
        {
            return "";
        }

        return "EXEC usp_setUserContext '" + currentUsername.Replace('\'', ' ') + "'; ";
    }

} 

特别是在这种情况下,参数不在CommandText 中(即CommandText 只包含存储过程名称),所以我不想陷入对参数进行字符串操作的混乱事务。有没有简单的方法来处理这种情况?

这里有一些关于使用CONTEXT_INFO 实现类似事情的其他文章。似乎这个主题有很多变化,但没有达成一致的方法,而且许多技术是 EF6 之前的,所以没有 IDbCommandInterceptor 选项。

更新

我猜是因为 it's not possible to get the 'SQL' that's executed 当 CommandType = StoredProcedure 时无法操纵它。我想到的选项(对于我想要设置 CONTEXT_INFO 的场景)是获取 command.Connection 并执行单独的命令来设置 CONTEXT_INFO。我不确定这会有多好,例如如果连接尚未打开,那么我必须自己打开它。

From .NET can I get the full SQL string generated by a SqlCommand object (with SQL Parameters)?

【问题讨论】:

  • 在这种情况下,特别是参数不在 CommandText 中我不明白你的意思。这将有助于了解您如何运行存储过程。
  • command.CommandText 看起来像 "usp_EmployeeUpdate"(即它只包含 proc 名称)。 command.Parameters 包含参数名称和值。在我的特定示例中,proc 由实体框架调用,自动生成代码,但可能是您使用command.CommandType = CommandType.StoredProcedure 的任何其他情况。
  • 例如return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("MyEntities.usp_EmployeeUpdate", idParameter, billingIdParameter, ... &lt;other params&gt; ... );

标签: c# sql-server entity-framework


【解决方案1】:

根据我上面的更新,我认为这是不可能的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多