Fluent NHibernate 有很多外键映射问题
我正在尝试在 nhibernate
Tables:
Employees
employeeID int
username varchar(30)
departmentID int
Departments
departmentID int
deptName varchar(50)
中映射一个简单的数据结构我的部门映射如下所示:
public class DepartmentMap: ClassMap<Department>
{
public DepartmentMap()
{
Id(m => m.DepartmentID).Column("departmentID");
Map(m => m.DepartmentName).Column("deptName").Length(50);
HasMany(m => m.Employees);
Table("Departments");
}
}
员工映射
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Id(x => x.EmployeeID, "employeeID");
Map(x => x.UserName, "username").Length(30);
References<Department>(x => x.Department, "departmentID");
Table("Employees");
}
}
... 以及我试图循环各部门并提取每个部门的所有员工的 :
foreach (var i in _directoryData.DepartmentCollection)
{
foreach (var e in i.Employees)
{
Debug.WriteLine(i.DepartmentName + " " + e.UserName);
}
}
这给了我一个错误,指出“无效的列名'Department_id'。
”......并且在生成的查询中它也使用department_id。当我循环遍历部门并输出部门名称时,它工作正常。
知道我缺少什么来获取部门 ID 的正确列名称吗?这是我的模型对象,以防万一:
public class Department
{
public virtual int DepartmentID { get; set; }
public virtual string DepartmentName { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
public class Employee : PersistentEntity
{
public virtual int EmployeeID { get; set; }
public virtual string UserName { get; set; }
public virtual Department Department { get; set; }
}
I'm trying to map a simple data structure in nhibernate
Tables:
Employees
employeeID int
username varchar(30)
departmentID int
Departments
departmentID int
deptName varchar(50)
My department mapping is like so:
public class DepartmentMap: ClassMap<Department>
{
public DepartmentMap()
{
Id(m => m.DepartmentID).Column("departmentID");
Map(m => m.DepartmentName).Column("deptName").Length(50);
HasMany(m => m.Employees);
Table("Departments");
}
}
... and the employee mapping
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Id(x => x.EmployeeID, "employeeID");
Map(x => x.UserName, "username").Length(30);
References<Department>(x => x.Department, "departmentID");
Table("Employees");
}
}
I'm trying to loop through departments and pull all employees from each department:
foreach (var i in _directoryData.DepartmentCollection)
{
foreach (var e in i.Employees)
{
Debug.WriteLine(i.DepartmentName + " " + e.UserName);
}
}
which gives me an error stating "Invalid column name 'Department_id'.
" ... and in the generated query it uses department_id as well. When I just loop through departments and output the department name it works fine.
Any idea what I'm missing to get the correct column name for departmentID? Here are my model objects just in case:
public class Department
{
public virtual int DepartmentID { get; set; }
public virtual string DepartmentName { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
public class Employee : PersistentEntity
{
public virtual int EmployeeID { get; set; }
public virtual string UserName { get; set; }
public virtual Department Department { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以: 创建 Fluent NHibernate 约定,以便将
HasMany
“外键”创建为<'Name'>ID
。或者更改部门映射:
You may either: create a Fluent NHibernate convention so that the
HasMany
"foreign key" is created as<'Name'>ID
.Or change the Department mapping:
您需要指定键列。
You need to specify the key column.
您需要在 HasMany 声明上使用
KeyColumn
方法,如 文档You need to use the
KeyColumn
method on the HasMany declaration, as explained in the documentation