如何从Devforce IdeaBlade应用程序设置context_info sql语句 [英] how can i set the context_info sql statement from Devforce IdeaBlade app

查看:100
本文介绍了如何从Devforce IdeaBlade应用程序设置context_info sql语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从winforms应用程序中设置Context_info,以便如果我的应用程序要保存记录而不是需要运行触发器的旧版应用程序,则可以通知数据库不要运行触发器.我已阅读的所有内容都表明需要使用数据上下文进行设置.

I need to set the Context_info from my winforms application so that i can notify the database not to run a trigger if my application is saving a record vs a legacy app which needs to run the trigger. Everything i have read says it needs to be set using a data context.

在我的应用程序中,我正在使用实体管理器.如何使用实体管理器而不是datacontext设置数据上下文.我只希望触发器知道它是我的应用程序正在运行并保存设置了触发器的数据

In my application i am using an entity manager. How can i set the data context using an entity manager instead of a datacontext. I just want the trigger to know that it is my app running and saving the data on which the trigger is set

我想做以下事情. 设置context_info'0x1234'

i want to do like the follow. "set context_info '0x1234'

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

at the start of the trigger i check to see if the context_info is set and dont run the trigger. The legacy does not set the context_info.

推荐答案

我们需要为我们的应用执行相同的操作.正如Kim所言,在Dev Force论坛中有很多信息.您可以找到有关我们所做操作的完整说明

We needed to do this same thing for our app. As Kim mentioned, there is a lot of information in the Dev Force forums. You can find a full explanation of what we did in this forum post but I'll reproduce the important parts here for reference...

我们在应用程序中有类似的要求.在我们的例子中,每次打开数据库连接时,我们都需要调用一个自定义存储过程-该过程将标记"该连接与当前处于活动状态的用户,因为我们有很多触发器需要了解当前用户(我们使用context_info来完成此标记".

We had a similar requirement in our application. In our case, we needed to call a custom stored procedure every time a DB connection was opened - the procedure would 'mark' the connection with what user was currently active because we have lots of triggers that need to know the current user (we use context_info to accomplish this 'marking').

我们能够借助 EF Provider Wrapper Toolkit (似乎也可以使用现在 Nuget .基本上,这使您可以将自己的逻辑注入到各种ADO.NET对象中-从而使数据库访问处于最低级别.然后,我们制作了自己的自定义DbConnection类,DevForce/EntityFramework最终使用了该类.实际上,这很容易,并且使我们很方便地进入了最低的数据库访问级别,这很方便.

We were able to handle this with the help of the EF Provider Wrapper Toolkit (also seems to be on Nuget now). That basically lets you inject your own logic into various ADO.NET objects - so at the very lowest level of database access. We then made our own custom DbConnection class that DevForce/EntityFramework end up using. It was actually pretty easy and has given us a lot of nice 'hooks' into the lowest level of database access that has come in handy a lot.

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

Here is some sample code for our custom DbConnection class that shows the kinds of things you can accomplish:

/// <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及更高版本中,可能有一种更干净的方法来拦截数据库调用....但是这种方法已经使用了很多年了.

In EF6 and beyond, there might be a cleaner way to intercept database calls....but this approach has been working great for years.

这篇关于如何从Devforce IdeaBlade应用程序设置context_info sql语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆