我正在爲Employee和Department兩個對象創建數據模型。員工有一個部門清單,部門有一個員工清單。如何保持與nhibernate同步的多對多關係?
class Employee{
private IList<Department> _departments;
public Employee()
{
_departments = new List<Department>();
}
public virtual ReadOnlyCollection<Department> Departments{
get {return new ReadOnlyCollection<Department>(_departments);}
}
}
class Department{
private IList<Employee> _employees;
public Department()
{
_departments = new List<Employee>();
}
public virtual ReadOnlyCollection<Employee> Employees{
get {return new ReadOnlyCollection<Employee>(_employees);}
}
}
如何編寫Department類中的AddEmployee方法和Employee類中的AddDepartment方法以使其與nHibernate同步? 我在Employee類
public virtual void AddDepartment(Department department)
{
if (!department.Employees.Contains(this))
{
department.Employees.Add(this);
}
_departments.Add(department);
}
寫這篇但正如我預料到work.Can別人幫助它不工作。
我已經發布了一個關於我如何處理這些場景的例子,但爲什麼你的代碼不工作預期?你在期待什麼? – 2012-04-11 01:51:25
當我試圖保存這個像db.session.Save(Employee)時會導致循環依賴;它歸屬於有部門的員工,部門內部有員工等。這是循環的。我不確定這是否正確。如果我錯了,有人能糾正我嗎? – 8GB 2012-04-11 13:58:22