首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Devforce IdeaBlade应用程序中设置context_info sql语句

如何在Devforce IdeaBlade应用程序中设置context_info sql语句
EN

Stack Overflow用户
提问于 2014-06-12 01:20:52
回答 2查看 2.5K关注 0票数 3

我需要在winforms应用程序中设置Context_info,以便在应用程序保存记录时通知数据库不要运行触发器,而不是需要运行触发器的遗留应用程序。我读到的所有内容都表明它需要使用数据上下文进行设置。

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

我想像下面这样做。“设置context_info '0x1234‘

在触发器开始时,我检查是否设置了context_info,并且不运行触发器。旧版本不设置context_info。

EN

回答 2

Stack Overflow用户

发布于 2014-06-21 04:50:46

我们需要为我们的应用程序做同样的事情。正如Kim提到的,在Dev Force论坛上有很多信息。你可以找到我们所做的in this forum post的完整解释,但我将在这里重现重要部分以供参考……

我们在应用程序中也有类似的要求。在我们的示例中,我们需要在每次打开数据库连接时调用一个自定义存储过程-该过程将使用当前活动的用户‘标记’该连接,因为我们有许多需要知道当前用户的触发器(我们使用context_info来完成此‘标记’)。

我们能够在EF Provider Wrapper Toolkit (现在似乎也在Nuget上)的帮助下处理这个问题。这基本上允许您将自己的逻辑注入到各种ADO.NET对象中-因此在最低级别的数据库访问中也是如此。然后,我们创建了自己的自定义DbConnection类,供DevForce/EntityFramework最终使用。它实际上非常简单,并且为我们提供了许多很好的“钩子”到最低级别的数据库访问,这已经派上了很大的用场。

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

代码语言:javascript
复制
/// <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和更远的地方,可能有一种更干净的方法来拦截数据库calls....but这种方法多年来一直运行得很好。

票数 3
EN

Stack Overflow用户

发布于 2014-06-12 05:44:09

尽管IdeaBlade forums已经对新的活动关闭,但它们仍然是可搜索的,并且确实经常包含关于DevForce问题的有用答案和信息。在这种情况下,如果你在那里搜索context_info,你会找到一些有用的线程。其中一个特别展示了如何使用EF Provider Wrapper ToolkitEF 6 DbCommandInterceptor与context_info一起工作。这些不需要DbContext。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24168904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档