title: Unit of Work Not Saving On POST Call
body: <p>I am following the Repository pattern and implementing UnitOfWork.</p>
  <p><strong>Here is the issue:</strong></p>
  <p>Within the <code>EmployeeController</code>, there is a post call titled <code>AddEmployee()</code>. This action method gets the appropriate data and the result comes back successful, but <strong>the data is not getting saved to the database</strong>. The action method is also calling the <code>SaveEmployee()</code> method, which should in theory save the data.</p>
  <blockquote>
  <p><strong>Git Repo:</strong> <a href="https://bitbucket.org/ChaseHardin/myapp" rel="nofollow noreferrer">https://bitbucket.org/ChaseHardin/myapp</a></p>
  <p><strong>Question:</strong> Why isn't the UnitOfWork saving the database changes?</p>
  </blockquote>
  <p><strong>Controller:</strong></p>
  <pre><code>[HttpPost]
  public HttpResponseMessage AddEmployee([FromBody]Employee employee)
  {
      if (ModelState.IsValid)
      {
           _employeeService.AddEmployee(employee);
           _employeeService.SaveEmployee();
  
           return new HttpResponseMessage(HttpStatusCode.OK);
      }
  
           return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
  }
  </code></pre>
  <p><strong>Service:</strong></p>
  <pre><code>  public class EmployeeService : IEmployeeService
      {
          private readonly IEmployeeRepository _employeeRepository;
          private readonly IUnitOfWork _unitOfWork;
  
          public EmployeeService(IUnitOfWork unitOfWork, IEmployeeRepository employeeRepository)
          {
              _unitOfWork = unitOfWork;
              _employeeRepository = employeeRepository;
          }
  
          public Employee GetEmployee(int id)
          {
              return _employeeRepository.GetById(id);
          }
  
          public void SaveEmployee()
          {
              _unitOfWork.Commit();
          }
  
          public void AddEmployee(Employee employee)
          {
              _employeeRepository.Add(employee);
          }
      }
  
      public interface IEmployeeService
      {
          Employee GetEmployee(int id);
          void SaveEmployee();
          void AddEmployee(Employee employee);
      }
  </code></pre>
  <p><strong>UnitOfWork</strong></p>
  <pre><code> public class UnitOfWork : IUnitOfWork
   {
       private readonly IDbFactory dbFactory;
       private MyAppEntities dbContext;
  
       public UnitOfWork(IDbFactory dbFactory)
       {
           this.dbFactory = dbFactory;
       }
  
       public MyAppEntities DbContext
       {
           get { return dbContext ?? (dbContext = dbFactory.Init()); }
       }
  
       public void Commit()
       {
           DbContext.Commit();
       }
   }
  
   public interface IUnitOfWork
   {
       void Commit();
   }
  </code></pre>
  <p><strong>MyAppEntities:</strong></p>
  <pre><code>  public class MyAppEntities : DbContext
    {
        public MyAppEntities() : base(&quot;MyAppEntities&quot;) { }
  
        public DbSet&lt;Employee&gt; Employees { get; set; }
  
        public virtual void Commit()
        {
            SaveChanges();
        }
    }
  </code></pre>
  <p><strong>EmployeeRepository</strong></p>
  <pre><code> public class EmployeeService : IEmployeeService
      {
          private readonly IEmployeeRepository _employeeRepository;
          private readonly IUnitOfWork _unitOfWork;
  
          public EmployeeService(IUnitOfWork unitOfWork, IEmployeeRepository employeeRepository)
          {
              _unitOfWork = unitOfWork;
              _employeeRepository = employeeRepository;
          }
  
          public Employee GetEmployee(int id)
          {
              return _employeeRepository.GetById(id);
          }
  
          public void SaveEmployee()
          {
              _unitOfWork.Commit();
          }
  
          public void AddEmployee(Employee employee)
          {
              _employeeRepository.Add(employee);
          }
      }
  
      public interface IEmployeeService
      {
          Employee GetEmployee(int id);
          void SaveEmployee();
          void AddEmployee(Employee employee);
      }
  </code></pre>
  <p><strong>Base Repo</strong></p>
  <pre><code> public abstract class BaseRepository &lt;T&gt; where T : class
      {
          private MyAppEntities _dataContext;
          private readonly IDbSet&lt;T&gt; _dbSet;
          protected IDbFactory DbFactory { get; private set; }
          protected MyAppEntities DbContext
          {
              get { return _dataContext ?? (_dataContext = DbFactory.Init()); }
          }
  
          protected BaseRepository(IDbFactory dbFactory)
          {
              DbFactory = dbFactory;
              _dbSet = DbContext.Set&lt;T&gt;();
          }
  
          #region Implementation
          public virtual void Add(T entity)
          {
              _dbSet.Add(entity);
          }
  
          public virtual void Update(T entity)
          {
              _dbSet.Attach(entity);
              _dataContext.Entry(entity).State = EntityState.Modified;
          }
  
          public virtual void Delete(T entity)
          {
              _dbSet.Remove(entity);
          }
  
          public virtual void Delete(Expression&lt;Func&lt;T, bool&gt;&gt; where)
          {
              IEnumerable&lt;T&gt; objects = _dbSet.Where&lt;T&gt;(where).AsEnumerable();
              foreach (T obj in objects)
                  _dbSet.Remove(obj);
          }
  
          public virtual T GetById(int id)
          {
              return _dbSet.Find(id);
          }
  
          public virtual IEnumerable&lt;T&gt; GetAll()
          {
              return _dbSet.ToList();
          }
  
          public virtual IEnumerable&lt;T&gt; GetMany(Expression&lt;Func&lt;T, bool&gt;&gt; where)
          {
              return _dbSet.Where(where).ToList();
          }
  
          public T Get(Expression&lt;Func&lt;T, bool&gt;&gt; where)
          {
              return _dbSet.Where(where).FirstOrDefault&lt;T&gt;();
          }
          #endregion
      }
  
      public interface IBaseRepository&lt;T&gt; where T : class
      {
          void Add(T entity);
          void Update(T entity);
          void Delete(T entity);
          void Delete(Expression&lt;Func&lt;T, bool&gt;&gt; where);
          T GetById(int id);
          T Get(Expression&lt;Func&lt;T, bool&gt;&gt; where);
          IEnumerable&lt;T&gt; GetAll();
          IEnumerable&lt;T&gt; GetMany(Expression&lt;Func&lt;T, bool&gt;&gt; where);
      }
  </code></pre>