实体框架4交易范围 [英] Entity Framework 4 Transaction Scope

查看:121
本文介绍了实体框架4交易范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个函数中运行2个INSERTS。第一个是非实体框架INSERT(AD0.NET)。第二个是EntityContext.SaveChanges()



我可以不将这两个嵌套在事务范围内?

解决方案

正如Eranga所说,这是完全可行的,如果有些混乱。



请参阅这个链接,以放心在TransactionScope



还要考虑为事务范围设置隔离级别,例如

  TransactionOptions options = new TransactionOptions(); 
options.IsolationLevel = IsolationLevel.Serializable;

使用(TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,options))
{
//在这里执行某些操作
}

两个不同的数据库插入(两个不同的并发连接)将导致事务镜像升级到分布式事务。使用 DTCPIng.exe 进行测试,它将在机器在问题。



PS不同的数据库有自己的默认隔离级别,所以最好的做法是在代码中指定这一点。 SQL Server Express默认使用Serializable,SQL Server(完整版)不支持!


I am running 2 INSERTS inside a function. The first one is a non-entity framework INSERT (AD0.NET). The second one is an EntityContext.SaveChanges()

Can I not nest both of these inside a Transaction Scope ?

解决方案

As Eranga says it is perfectly feasible, if somewhat messy.

See this link for peace of mind on TransactionScope

Also consider setting the isolationlevel for the transaction scope e.g.

TransactionOptions options = new TransactionOptions();
options.IsolationLevel = IsolationLevel.Serializable;

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
{
// Do something here
}

Two different database inserts (two different concurrent connections) will cause transactionscope to escalate to a distributed transaction. Use DTCPIng.exe to test it will work between the machines in questions.

P.S. Different databases have thier own default isolation levels, so it's best practice to specify this in your code, e.g. SQL Server Express uses Serializable by default, SQL Server (Full Version) does not!

这篇关于实体框架4交易范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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