TransactionScope and SQL Server Compact

北慕城南 提交于 2019-12-24 10:06:25

问题


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?

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

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.


回答1:


Updated answer (code sample from here):

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

Update

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.




回答2:


My error was that I passed connection string to ObjectContext. If I pass connection object, only one connection is used.



来源:https://stackoverflow.com/questions/6690475/transactionscope-and-sql-server-compact

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!