为什么数据库中的数据没有被更新但是对象却被更新并且没有错误?

Ste*_*eve 6 c# entity-framework unit-of-work repository-pattern entity-framework-core

我有这个银行ATM样机应用程序,它实现了一些域驱动的设计架构和工作单元模式。

这个程序有3个基本功能:

  • 查看余额
  • 存款
  • 退出

这些是项目层:

ATM.Model(域模型实体层)

namespace ATM.Model
{
public class BankAccount
{
    public int Id { get; set; }
    public string AccountName { get; set; }
    public decimal Balance { get; set; }

    public decimal CheckBalance()
    {
        return Balance;
    }

    public void Deposit(int amount)
    {
        // Domain logic
        Balance += amount;
    }

    public void Withdraw(int amount)
    {
        // Domain logic
        //if(amount > Balance)
        //{
        //    throw new Exception("Withdraw amount exceed account balance.");
        //}

        Balance -= amount;
    }
}
}

namespace ATM.Model
{
public class Transaction
{
    public int Id { get; set; }
    public int BankAccountId { get; set; }
    public DateTime TransactionDateTime { get; set; }
    public TransactionType TransactionType { get; set; }
    public decimal Amount { get; set; }
}

public enum TransactionType
{
    Deposit, Withdraw
}
}
Run Code Online (Sandbox Code Playgroud)

ATM.Persistence(持久层)

namespace ATM.Persistence.Context
{
public class AppDbContext : DbContext
{        
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"[connstring]");
    }

    public DbSet<BankAccount> BankAccounts { get; set; }
    public DbSet<Transaction> Transactions { get; set; }
}
}

namespace ATM.Persistence.Repository
{
public class RepositoryBankAccount
{
    public AppDbContext context { get; }

    public RepositoryBankAccount()
    {
        context = new AppDbContext();
    }

    public BankAccount FindById(int bankAccountId)
    {
        return context.BankAccounts.Find(bankAccountId);
    }

    public void AddBankAccount(BankAccount account)
    {
        context.BankAccounts.Add(account);
    }

    public void UpdateBankAccount(BankAccount account)
    {
        context.Entry(account).State = EntityState.Modified;
    }
}
}

namespace ATM.Persistence.Repository
{
public class RepositoryTransaction
{
    private readonly AppDbContext context;

    public RepositoryTransaction()
    {
        context = new AppDbContext();
    }

    public void AddTransaction(Transaction transaction)
    {
        context.Transactions.Add(transaction);
    }
}
}

namespace ATM.Persistence.UnitOfWork
{
public class UnitOfWork : IUnitOfWork
{
    private readonly AppDbContext db;
    public UnitOfWork()
    {
        db = new AppDbContext();
    }

    private RepositoryBankAccount _BankAccounts;
    public RepositoryBankAccount BankAccounts
    {
        get
        {
            if (_BankAccounts == null)
            {
                _BankAccounts = new RepositoryBankAccount();
            }
            return _BankAccounts;
        }
    }

    private RepositoryTransaction _Transactions;
    public RepositoryTransaction Transactions
    {
        get
        {
            if (_Transactions == null)
            {
                _Transactions = new RepositoryTransaction();
            }
            return _Transactions;
        }
    }

    public void Dispose()
    {
        db.Dispose();
    }

    public int Commit()
    {
        return db.SaveChanges();
    }

    public void Rollback()
    {
        db
        .ChangeTracker
        .Entries()
        .ToList()
        .ForEach(x => x.Reload());
    }
}
}
Run Code Online (Sandbox Code Playgroud)

ATM.ApplicationService(应用程序层)

namespace ATM.ApplicationService
{
public class AccountService
{        
    private readonly UnitOfWork uow;

    public AccountService()
    {            
        uow = new UnitOfWork();
    }

    public void DepositAmount(BankAccount bankAccount, int amount)
    {            
        bankAccount.Deposit(amount);
        uow.BankAccounts.UpdateBankAccount(bankAccount);

        var transaction = new Transaction()
        {
            BankAccountId = bankAccount.Id,
            Amount = amount,
            TransactionDateTime = DateTime.Now,
            TransactionType = TransactionType.Deposit
        };

        uow.Transactions.AddTransaction(transaction);

        try
        {
            uow.Commit();
        }
        catch
        {
            uow.Rollback();
        }
        finally
        {
            uow.Dispose();
        }
    }

    public void WithdrawAmount(BankAccount bankAccount, int amount)
    {            
        bankAccount.Withdraw(amount);
        uow.BankAccounts.UpdateBankAccount(bankAccount);
        //repoBankAccount.UpdateBankAccount(bankAccount);

        var transaction = new Transaction()
        {
            BankAccountId = bankAccount.Id,
            Amount = amount,
            TransactionDateTime = DateTime.Now,
            TransactionType = TransactionType.Withdraw
        };

        uow.Transactions.AddTransaction(transaction);

        try
        {
            uow.Commit();
        }
        catch
        {
            uow.Rollback();
        }
        finally
        {
            uow.Dispose();
        }
    }

    public decimal CheckBalanceAmount(int bankAccountId)
    {
        BankAccount bankAccount = uow.BankAccounts.FindById(bankAccountId);

        return bankAccount.CheckBalance();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

ATM控制台UICore

namespace ATM.ConsoleUICore
{
class Program
{
    static void Main()
    {
        AccountService accountService = new AccountService();
        RepositoryBankAccount repoBankAccount = new RepositoryBankAccount();

        var bankAccount = repoBankAccount.FindById(2);

        Console.WriteLine("1. Check balance");
        Console.WriteLine("2. Deposit");
        Console.WriteLine("3. Withdraw");
        Console.WriteLine("Enter option: ");
        string opt = Console.ReadLine();
        switch (opt)
        {
            case "1":
                Console.WriteLine($"Your balance is ${bankAccount.CheckBalance()}");
                break;
            case "2":
                // User to input amount.
                // Data validation to make sure amount is greater than zero.
                // Pass the input amount to Application layer.

                accountService.DepositAmount(bankAccount, 50);

                // After getting the operation status from Application service layer.
                // Print operation status here: Either success or fail
                Console.WriteLine("Deposit successfully");
                break;
            case "3":            
                break;
            default:
                break;
        }

    }
}
}
Run Code Online (Sandbox Code Playgroud)

我可以成功检查余额。对于选项2,我可以执行“存款”选项而不会出现任何错误。但是在数据库中,我的余额余额未更新。事务也未添加到数据库中。

如果我把回来context.SaveChanges();UpdateBankAccount方法,它的工作原理。它返回1。但是,我使用UoW执行SaveChanges()。在SaveChanges()没有在UOW执行Commit方法,但数据库没有反映其变化。UoW Commit方法SaveChanges返回0。

完整的代码可以在Github仓库中找到。

Ami*_*shi 6

问题的核心在于,AppDbContext正在创建两个实例来执行一个动作。更改在一个实例中进行,SaveChanges而在另一实例中被调用。显然,它没有反映在基础数据库中。

现在,我们将从下至上逐步介绍您的代码。

ATM.ConsoleUICore.Program.Main()方法中,请注意以下代码:

AccountService accountService = new AccountService();
...
...
...
accountService.DepositAmount(bankAccount, 50);
Run Code Online (Sandbox Code Playgroud)

您正在创建的实例AccountService。在的构造函数中AccountService,您将创建的实例,UnitOfWork如下所示:

private readonly UnitOfWork uow;
public AccountService()
{            
    uow = new UnitOfWork();
}
Run Code Online (Sandbox Code Playgroud)

在的构造函数中UnitOfWork,您正在创建的实例AppDbContext(从派生DbContext)。
您还在BankAccounts那里拥有一个实例,RepositoryBankAccount如下所示:

private readonly AppDbContext db;
public UnitOfWork()
{
    db = new AppDbContext();
}
...
...
...
private RepositoryBankAccount _BankAccounts;
public RepositoryBankAccount BankAccounts
{
    get
    {
        if (_BankAccounts == null)
        {
            _BankAccounts = new RepositoryBankAccount();
        }
        return _BankAccounts;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在的问题是...

在的构造函数中RepositoryBankAccount,您将再次创建如下的实例AppDbContext

public AppDbContext context { get; }
public RepositoryBankAccount()
{
    context = new AppDbContext();
}
Run Code Online (Sandbox Code Playgroud)

实际上,您冒充一个UnitOfWork实例下的操作作为一个数据库事务执行。但是,当您AppDbContext在存储库中创建不同的实例时,情况并非如此。您的工作单元已从存储库中分离出来。您必须连接它们。它应该是AppDbContext到处都是相同的实例。

那么,解决方案是什么?

不要AppDbContext在任何存储库中创建的实例。而是从工作单元中注入现有实例。

public AppDbContext context { get; }

public RepositoryBankAccount(AppDbContext appDbContext)//<==Inject the AppDbContext
{
    context = appDbContext;//<==Do NOT create new instance here; assign the injected instance.
}
Run Code Online (Sandbox Code Playgroud)

然后,在您的UnitOfWork班级中,如下更改属性BankAccounts

private RepositoryBankAccount _BankAccounts;
public RepositoryBankAccount BankAccounts
{
    get
    {
        if (_BankAccounts == null)
        {
            _BankAccounts = new RepositoryBankAccount(db);//<==Note that `db` means `AppDbContext` is injected
        }
        return _BankAccounts;
    }
}
Run Code Online (Sandbox Code Playgroud)

顺便说一下,请避免将所有这些不必要的包装纸都放在包装纸上。

看一下这个答案,解释为什么不需要这种包装。

万一您决定继续使用现有设计,我已经在上面提出了一个解决方案。

另外,我建议您的一个工作单元应该是一个数据库事务。因此,数据库事务在创建工作单元的实例时开始,而在处理它时结束(提交或回滚)。要么所有内容都刷新到数据库,要么不刷新。在这之间发生的所有事情都应该是一个数据库事务的一部分。万一发生异常,请一起回滚工作单元。