Does anybody know how to create an explicit transaction in Entity Framework 7 ??? All the info I find is refered to the version 6 of EF. The documentation is also very incomplete so, could anybody provide an example of it?
I have a dbContext and I must delete an entity and it's related objects, and then insert them again, but in the same transaction, so I'll always have a "version" of the rows.
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.