将 LINQ 事务设置为指定查询(不使用 SqlCommand)
我很难正确指定问题的标题 - 本质上我收到一条错误消息“当分配给命令的连接处于挂起的本地事务中时,ExecuteReader 要求命令有一个事务。命令的 Transaction 属性没有已初始化。”对于与此类似的情况:
using (db = getDbContext())
{
var results = (from t in db.table
select t.column).SingleOrDefault();
}
正如错误所述,所有这些都已经包含在另一个事务中,我也尝试将其用于此查询。
如何使用这种查询格式指定交易?
我尝试创建一个 SqlCommand("select column from table", myconnection, mytransaction)
, 这是可行的,但我更愿意使用 LINQ 语法来获得智能感知(除其他外)的好处,因为实际的查询相当复杂。
非常感谢,这已经让我烦恼了几个小时。
亚历克斯
I was having difficulty specifying the title for the question properly - essentially I am getting an error saying "ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized." for a situation similar to this:
using (db = getDbContext())
{
var results = (from t in db.table
select t.column).SingleOrDefault();
}
As the error says, all this is already wrapped in another transaction, which I am trying to use for this query as well.
How can I specify the transaction using this query format?
I've tried creating an SqlCommand("select column from table", myconnection, mytransaction)
,
which works, but I'd much rather use the LINQ syntax for the intellisense (amongst other) benefits, as the actual query is rather more complex
Many thanks, this has been annoying me for hours.
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将事务设置到上下文本身中:
You can set the transaction into the context itself: