我正在为两个对象 Employee 和 Department 创建数据模型。Employee 有一个部门列表,Department 有一个员工列表。
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);
}
但它不像我预期的那样工作。有人可以帮忙。