无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象。 C#

时间:2017-01-09 22:21:35

标签: c# asp.net-mvc entity-framework one-to-many unit-of-work

我有以下代码

 public void SetDeparment(Guid employeeId,Guid departmentId)
        {
            var department = DepartmentRepository.GetById(departmentId);
            Ensure.That(department).IsNotNull();
            var employee = EmployeeRepository.GetById(employeeId);
            department.AddEmployee(employee);
            if(employee.Department==null)
            {
                employee.Department = new Department();
            }
            employee.Department = department;
            EmployeeRepository.Update(employee);
            UnitOfWork.Commit();
        }

当我尝试调用此方法时,它会给出标题

中显示的错误

在存储库更新中,我有:

   public void Update(T entity)
        {
            DataBaseContext.Set<T>().AddOrUpdate(entity);
        }

任何人都可以帮助我吗?感谢。

0 个答案:

没有答案