The transaction functionality are included in EF7 starting with RC1 only (see the statement). The call of .SaveChanges()
(or .SaveChangesAsync()
) should use automatic transaction. Then I suppose that the state of some items of the entities should be marked as Deleted before, for example.
One can start transaction explicitly by wrapping some fragment of manipulation on the database inside of
using (context.Database.BeginTransaction()) {
/*do something*/
}
The transaction will be committed by call of .Dispose()
at the end of using
block. One can use alternatively
using (var transaction = await context.Database.BeginTransactionAsync()) {
/*do something*/
/*one can call
transaction.Rollback() or transaction.Commit()
explicitly in the code */
}
It general all should looks like in previous version of Entity Framework. See the post for example.
dbContext
orDbCommand
? Typically one should useusing (context.Database.BeginTransaction()) {.../*do something*/...}
, but one can useawait context.Database.BeginTransactionAsync()
instead. One can useusing (var transaction = context.Database.BeginTransaction()) {...}
and calltransaction.Commit()
explicitly to commit transaction or calltransaction.Rollback()
for rollback. There are exist more exotic EnlistTransaction.