我如何从Devforce IdeaBlade应用程序设置context_info sql语句

本文关键字:context info sql 语句 设置 应用程序 Devforce IdeaBlade | 更新日期: 2023-09-27 18:08:06

我需要从我的winforms应用程序设置Context_info,以便我可以通知数据库不运行触发器,如果我的应用程序保存记录vs需要运行触发器的遗留应用程序。我读到的所有东西都说它需要使用数据上下文来设置。

在我的应用程序中,我使用实体管理器。如何使用实体管理器而不是数据上下文来设置数据上下文?我只是想让触发器知道这是我的应用程序正在运行并保存触发器设置的数据

我想这样做。"set context_info '0x1234'

在触发器开始时,我检查是否设置了context_info,并且不运行触发器。legacy没有设置context_info.

我如何从Devforce IdeaBlade应用程序设置context_info sql语句

我们需要为我们的应用做同样的事情。正如Kim所提到的,在Dev Force论坛上有很多信息。你可以在这个论坛帖子中找到我们所做的完整解释,但我将在这里复制重要部分以供参考…

在我们的应用程序中也有类似的需求。在我们的例子中,每次打开一个DB连接时,我们需要调用一个自定义存储过程——这个过程将用当前活动的用户来"标记"这个连接,因为我们有很多需要知道当前用户的触发器(我们使用context_info来完成这个"标记")。

我们能够在EF提供者包装工具包的帮助下处理这个问题(现在似乎也在Nuget上)。这基本上允许您将自己的逻辑注入到各种ADO中。NET对象——所以在数据库访问的最低层次。然后,我们制作了DevForce/EntityFramework最终使用的自定义DbConnection类。它实际上非常简单,并且为我们提供了很多很好的"钩子"到最低级别的数据库访问,这已经派上了用场。

下面是我们自定义DbConnection类的一些示例代码,展示了您可以完成的各种事情:

/// <summary>
/// Custom implementation of a wrapper to <see cref="DbConnection"/>.
/// Allows custom behavior at the connection level.
/// </summary>
internal class CustomDbConnection : DbConnectionWrapper
{
    /// <summary>
    /// Opens a database connection with the settings specified by 
    /// the <see cref="P:System.Data.Common.DbConnection.ConnectionString"/>.
    /// </summary>
    public override void Open()
    {
        base.Open();
        //After the connection has been opened, do our logic to prep the connection
        SetContextInfo();
        //...and we do some other stuff not relevant to this discussion
    }
    /// <summary>
    /// Closes the connection to the database. This is the preferred method of closing any open connection.
    /// </summary>
    /// <exception cref="T:System.Data.Common.DbException">
    /// The connection-level error that occurred while opening the connection.
    /// </exception>
    public override void Close()
    {
        //Before closing, we do some cleanup with the connection to make sure we leave it clean
        //   for the next person that might get it....
        base.Close();
    }
    /// <summary>
    /// Attempts to set context_info to the current connection if the user is 
    /// logged in to our application.
    /// </summary>
    private void SetContextInfo()
    {
        //See if a user is logged in
        var user = Thread.CurrentPrincipal as OurCustomUserType;
        //If not, we don't need to do anything - this is probably a very early call in the application
        if (user == null)
            return;
        //Create the ADO.NET command that will call our stored procedure
        var cmd = CreateCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "p_prepare_connection_for_use";
        //Set the parameters based on the currently logged in user
        cmd.CreateParameter("as_session_id", user.SessionID, null, DbType.Guid);
        cmd.CreateParameter("ai_user_sid", user.UserID, null, DbType.Int32);
        //Run the SP
        cmd.ExecuteNonQuery();
    }

在EF6及以后,可能有一个更干净的方式来拦截数据库调用....但是这种方法多年来一直很有效。

虽然IdeaBlade论坛已经关闭了新的活动,但它们仍然是可搜索的,并且经常包含有关DevForce问题的有用答案和信息。在这种情况下,如果您在那里搜索context_info,您将找到一些有用的线程。其中一个特别展示了如何使用EF Provider Wrapper Toolkit或EF 6 DbCommandInterceptor来处理context_info。这些不需要DbContext。