I would like know what is the best possible way to implement transactions with DBContext
. In particular,
DbContext.SaveChanges
implement transaction internall if i change multiple entities?DbContext.SaveChanges
multiple times(same contxet/different contxets), how transaction can be achieved?SaveChanges
uses transaction internally.TransactionScope
to wrap multiple calls to SaveChanges
Example:
using(var scope = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
{
// Do something
context.SaveChanges();
// Do something else
context.SaveChanges();
scope.Complete();
}