Transactions EF6不会回滚事务中的所有更改

Transactions EF6不会回滚事务中的所有更改,transactions,entity-framework-6,rollback,Transactions,Entity Framework 6,Rollback,我的理解是,除非调用transaction.Commit,否则所有操作都应该回滚到事务范围内。为什么考利昂最终没有破产 using (var ctx = new BankDbContext()) { using (var transaction = ctx.Database.BeginTransaction()) { try { var corleone = new BankAccount { Name = "Corleone", Balance = 100000M };

我的理解是,除非调用transaction.Commit,否则所有操作都应该回滚到事务范围内。为什么考利昂最终没有破产

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"));
    }
}}

它是在数据库本身中提交的,还是只是未能通过断言?数据库是否已经有一个同名的帐户?如果使用TransactionScope,它是否仍然提交?什么数据库?为什么?因为它不存在。也许你应该告诉我你期望会发生什么,什么不会发生。