为什么我的CONTEXT_INFO()是空的? [英] Why is my CONTEXT_INFO() empty?

查看:630
本文介绍了为什么我的CONTEXT_INFO()是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有设置我的LINQ数据上下文的方法。它返回的DC之前,它调用存储过程,设置了CONTEXT_INFO值来识别当前用户。

I have a method that sets up my linq data context. Before it returns the DC it calls a stored proc that sets up the CONTEXT_INFO value to identify the current user.

一个触发拿起所作的任何更改,并使用此上下文数据写入审计记录。

A trigger picks up any changes made and using this context data writes an audit record.

我发现我的上下文数据是审计表的空白,所以我写了一个简单的单元测试步骤通过这个过程,我仍然什么也得不到。然而,如果我粘贴所有的LINQ到SQL语句到一个查询窗口中的上下文数据是存在的。

I noticed that my context data was in the audit table blank so I wrote a simple unit test to step through this process and I still get nothing. However if I paste all the Linq-To-SQL statements into a query window the context data is there.

综观事件探查器跟踪它使相当在这个过程中的几个sp_reset_connection的电话。我了解,这些不应该对CONTEXT_INFO值虽然产生影响。

Looking at a profiler trace it makes quite a few sp_reset_connection calls in this process. I had understood that these should not have an affect on the CONTEXT_INFO value though.

那么是什么回事?

推荐答案

一个LINQ到SQL 的DataContext 实际上并不持有在执行查询的连接打开,或者使用查询COM prehension或的executeQuery / ExecuteMethod 通话和 CONTEXT_INFO 只住在一个单一连接的情况下。

A Linq to SQL DataContext does not actually hold the connection open when you execute queries, either using query comprehension or ExecuteQuery/ExecuteMethod call, and CONTEXT_INFO only lives in the context of a single connection.

为了得到这个工作,你需要手动打开的的DataContext 使用 context.Connection.Open()设置CONTEXT_INFO之前。一旦连接已经打开,后续查询不会自动关闭时,他们完成了连接。

In order to get this to work, you need to manually open the connection on the DataContext using context.Connection.Open() before setting the context_info. Once the connection is already open, successive queries won't auto-close the connection when they're finished.

请注意 - 的技术原因是,它调用的ExecuteReader IDbCommand的 CommandBehavior.CloseConnection 设置,除非连接已经打开。您可以自己看到相同的行为,如果你使用的SqlCommand / IDbCommand的对象具有相同标志设置。

Note - the technical reason for this is that it invokes ExecuteReader on the IDbCommand with CommandBehavior.CloseConnection set, unless the connection was already open. You can see the same behaviour yourself if you use SqlCommand/IDbCommand objects with the same flag set.

编辑 - 我想我也应该指出的是,如果连接池,在技术上物理连接开放的全部时间,但的IDbConnection 还是日渐关闭,这是什么原因造成的连接重置。

Edit - I guess I should also point out that if the connection is pooled, technically the physical connection is "open" the whole time, but the IDbConnection is still getting closed, which is what causes the connection resets.

这篇关于为什么我的CONTEXT_INFO()是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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