【发布时间】:2014-02-19 17:12:15
【问题描述】:
有没有办法确定打开的DbContext 的当前 SQL Server 会话 ID (@@SPID),而不是直接对数据库进行 SQL 查询?
如果有,是否保证 SQL Server 会话 ID 将保持不变,直到释放 DbContext 并将其连接释放回实体框架连接池?类似的东西:
using (MyEntities db = new MyEntities()) {
// the following 3 pieces of code are not existing properties and will result in compilation errors
// I'm just looking for something similar to the following 3 lines
db.CurrentSessionId; //error
db.Database.CurrentSessionId; //error
((IObjectContextAdapter)db).ObjectContext.Connection.CurrentSessionId; //error
// the following code will work, but will this session id be the same until the original DbContext is disposed?
// is there any chance that a db.Database.SqlQuery call will spin off it's own connection from the pool?
short spid = db.Database.SqlQuery<short>("SELECT @@SPID").FirstOrDefault();
}
【问题讨论】:
标签: c# sql sql-server entity-framework entity-framework-5