POST 调用不保存工作单元

Cha*_*din 2 c# asp.net entity-framework repository-pattern

我正在遵循存储库模式并实施 UnitOfWork。

这是问题:

在里面EmployeeController,有一个名为AddEmployee(). 此操作方法获取适当的数据,结果成功返回,但数据未保存到数据库中。action 方法也在调用SaveEmployee()方法,理论上应该保存数据。

Git 存储库: https : //bitbucket.org/ChaseHardin/myapp

问题:为什么 UnitOfWork 不保存数据库更改?

控制器:

[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);
}
Run Code Online (Sandbox Code Playgroud)

服务:

  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);
    }
Run Code Online (Sandbox Code Playgroud)

工作单位

 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();
 }
Run Code Online (Sandbox Code Playgroud)

我的应用实体:

  public class MyAppEntities : DbContext
  {
      public MyAppEntities() : base("MyAppEntities") { }

      public DbSet<Employee> Employees { get; set; }

      public virtual void Commit()
      {
          SaveChanges();
      }
  }
Run Code Online (Sandbox Code Playgroud)

员工资料库

 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);
    }
Run Code Online (Sandbox Code Playgroud)

基础回购

 public abstract class BaseRepository <T> where T : class
    {
        private MyAppEntities _dataContext;
        private readonly IDbSet<T> _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<T>();
        }

        #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<Func<T, bool>> where)
        {
            IEnumerable<T> objects = _dbSet.Where<T>(where).AsEnumerable();
            foreach (T obj in objects)
                _dbSet.Remove(obj);
        }

        public virtual T GetById(int id)
        {
            return _dbSet.Find(id);
        }

        public virtual IEnumerable<T> GetAll()
        {
            return _dbSet.ToList();
        }

        public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
        {
            return _dbSet.Where(where).ToList();
        }

        public T Get(Expression<Func<T, bool>> where)
        {
            return _dbSet.Where(where).FirstOrDefault<T>();
        }
        #endregion
    }

    public interface IBaseRepository<T> where T : class
    {
        void Add(T entity);
        void Update(T entity);
        void Delete(T entity);
        void Delete(Expression<Func<T, bool>> where);
        T GetById(int id);
        T Get(Expression<Func<T, bool>> where);
        IEnumerable<T> GetAll();
        IEnumerable<T> GetMany(Expression<Func<T, bool>> where);
    }
Run Code Online (Sandbox Code Playgroud)

Tim*_*imS 6

问题是您的 IOC 范围。在App_Start您将对象与默认的Transient Scope绑定。这会导致创建多个上下文,并且您在一个上下文中添加员工并调用SaveChanges()另一个上下文。

更新您的 Ninject 绑定以使用InSingletonScope()InRequestScope()