title: Why the database data is not being updated but object did and without error?
body: <p>I have this bank ATM mock-up app which implements some Domain-Driven Design architecture and Unit of Work pattern. </p>
  
  <p>This app have 3 basic functions:</p>
  
  <ul>
  <li>Check balance</li>
  <li>Deposit</li>
  <li>Withdraw</li>
  </ul>
  
  <p>These are the project layers:</p>
  
  <p><strong>ATM.Model (Domain model entity layer)</strong></p>
  
  <pre><code>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 &gt; 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
  }
  }
  </code></pre>
  
  <p><strong>ATM.Persistence (Persistence Layer)</strong></p>
  
  <pre><code>namespace ATM.Persistence.Context
  {
  public class AppDbContext : DbContext
  {        
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
          optionsBuilder.UseSqlServer(@"[connstring]");
      }
  
      public DbSet&lt;BankAccount&gt; BankAccounts { get; set; }
      public DbSet&lt;Transaction&gt; 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 =&gt; x.Reload());
      }
  }
  }
  </code></pre>
  
  <p><strong>ATM.ApplicationService (Application layer)</strong></p>
  
  <pre><code>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();
      }
  }
  }
  </code></pre>
  
  <p><strong>ATM.ConsoleUICore</strong></p>
  
  <pre><code>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;
          }
  
      }
  }
  }
  </code></pre>
  
  <p>I could check balance successfully. For option 2, I can execute "Deposit" option without any error. But in the database, my balance balance is not being updated. Transaction is also not added into the db.</p>
  
  <p>If I put back <code>context.SaveChanges();</code> in <code>UpdateBankAccount</code> method, it works. It returns 1. But, I use UoW to perform <code>SaveChanges()</code>. The <code>SaveChanges()</code> did executed in UoW Commit method but the database didn't reflect its changes. The UoW <code>Commit</code> method <code>SaveChanges</code> returns 0.</p>
  
  <p>Complete code can be found on <a href="https://github.com/ngaisteve1/ATM_OOP" rel="nofollow noreferrer">Github repository</a>.</p>