开发者

Setting LINQ transaction a specified query (without SqlCommand)

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 cre开发者_如何学Cating 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


You can set the transaction into the context itself:

db.Transaction = theTransaction;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜