EF代码第一个DBContext和事务 [英] EF Code First DBContext and Transactions
本文介绍了EF代码第一个DBContext和事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道使用 DBContext
实现交易的最佳方法是什么。特别是,如果我更改多个实体,则实现事务内部实现
I would like know what is the best possible way to implement transactions with DBContext
. In particular,
- 如果我更改多个实体,
DbContext.SaveChanges
? - 如果我想多次调用
DbContext.SaveChanges
(相同的contxet /不同的contxets),如何实现事务? / li>
- Does
DbContext.SaveChanges
implement transaction internall if i change multiple entities? - If i want to call
DbContext.SaveChanges
multiple times(same contxet/different contxets), how transaction can be achieved?
推荐答案
- 是的。
SaveChanges
在内部使用事务。 - 使用
TransactionScope
code> SaveChanges
- Yes.
SaveChanges
uses transaction internally. - Use
TransactionScope
to wrap multiple calls toSaveChanges
示例:
using(var scope = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
{
// Do something
context.SaveChanges();
// Do something else
context.SaveChanges();
scope.Complete();
}
这篇关于EF代码第一个DBContext和事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文