的TransactionScope和SQL Server Compact [英] TransactionScope and SQL Server Compact

查看:229
本文介绍了的TransactionScope和SQL Server Compact的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQL Server精简版不支持分布式事务。因此,如果有内部的TransactionScope多个连接 - 抛出异常。有什么办法来设置ADO.NET提供者使用相同的连接字符串一个连接?

SQL Server Compact doesn't support distributed transactions. So if there are more than one connection inside TransactionScope - the exception is thrown. Is there any way to setup ADO.NET provider to use one connection for the same connection string?

我明白我可以通过connection.BeginTransaction使用通常的事务,但是TransactionScope的是preferable我。

I understand I can use usual transactions through connection.BeginTransaction but TransactionScope is preferable for me.

更新
对不起,我没有提到我与实体框架工作,所以我对SQL命令进行控制。我可能只是通过连接字符串。而由于某种原因多个连接对象的内部TransactionScope的一个连接字符串创建。

UPDATE.
Sorry, I didn't mention I work with Entity Framework, so I have no control on SQL Command. I may just pass connection string. And by some reason several connections objects created for one connection string inside TransactionScope.

推荐答案

更​​新答案(由<一个code样品href="http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/c3fd7555-7fff-44b8-8e1e-030073c20597/"相对=nofollow>这里):

using (var context = new MyContext())
{
    using (var txscope = new TransactionScope())
    {
        context.Connection.Open();
        // do query 1
        // do query 2
    }
}


更新

另一种解决方案,如你所说,是创建一个连接对象,并用它在构造函数的对象上下文。

Another solution, as you said, is to create a connection object and use it in constructors for the Object Context.

更多信息有关的实体时,会打开新的连接。

More information about when Entities opens new connection.

这篇关于的TransactionScope和SQL Server Compact的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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