EntityFramework 6应使用哪个事务范围 [英] Which transaction scope should be used for EntityFramework 6
问题描述
您好,
我的项目使用WebAPI,EF 6.0和存储库模式,我想在项目中实现事务范围,我怀疑要使用哪个事务范围
注意:我只有一个数据库
1).Net(TransactionScope ts = new TransactionScope())
或
2)EF的交易范围(DbContext.Database.BeginTransaction())
哪一个更好或哪一个有优势?
Hello,
My project using WebAPI, EF 6.0 and repository pattern, I wanted to implement Transaction scope in project, I have doubt about which transaction scope to be used
Note : I have only one database
1) .Net (TransactionScope ts=new TransactionScope() )
OR
2) Transaction scope of EF(DbContext.Database.BeginTransaction())
which one is better or which one has advantages?
推荐答案
如果你只需要处理数据库事务,你应该选择第二个选项。
(注意,TransactionScope可以使代码块成为跨国的,所以它能够处理除数据库以外的交易也是如此)
参考: Database.BeginTransaction vs Transactions.TransactionScope [ ^ ]
update:
==== ==
you should go for 2nd option if you only need to handle database transactions.
(note that TransactionScope can make code block to a transnational, so it capable of handling transactions other than databases as well)
refer : Database.BeginTransaction vs Transactions.TransactionScope[^]
update:
======
我觉得第二个选项并不容易实现,因为我实现了Repository模式,First一个很容易实现,有第二个选项的好处或第一个的任何缺点
I feel second option is not that easy to implement as I have Repository pattern implemented, First one is easy to implement, Is there benefit of second option or any drawback of first one
检查存储库模式和工作单元与ASP.NET MVC中的实体框架 [ ^ ]示例代码
这篇关于EntityFramework 6应使用哪个事务范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!