我的理解是,除非调用transaction.Commit,否则应在事务范围内回滚所有操作。为什么Corleone最终没有破产?
using (var ctx = new BankDbContext()) {
using (var transaction = ctx.Database.BeginTransaction()) {
try {
var corleone = new BankAccount { Name = "Corleone", Balance = 100000M };
ctx.Accounts.Add(corleone);
ctx.SaveChanges();
Debug.Assert(corleone.Balance == 100000M);
corleone.Balance += 100000M;
ctx.SaveChanges();
Debug.Assert(corleone.Balance == 200000M);
throw new Exception("money laundering!");
transaction.Commit();
} catch (Exception e) {
transaction.Rollback();
Debug.Assert(!ctx.Accounts.Any(x => x.Name == "Corleone"));
}
}}