流利的NHibernate的HasMany外键映射问题 [英] Fluent NHibernate HasMany Foreign Key Mapping Problem

查看:119
本文介绍了流利的NHibernate的HasMany外键映射问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在nhibernate中映射一个简单的数据结构



表:

 员工
employeeID int
username varchar(30)
departmentID int

部门
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);

表(部门);


$ / code $ / pre
$ b $ p

和雇员映射

  public class EmployeeMap:ClassMap< Employee> 
{
public EmployeeMap()
{
Id(x => x.EmployeeID,employeeID);
Map(x => x.UserName,username)。Length(30);

参考< Department>(x => x.Department,departmentID);

表(雇员);




$ b我试图通过部门循环,每个部门的员工:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ foreach($ var $在i.Employees)
{
Debug.WriteLine(i.DepartmentName ++ e.UserName);






$ p $这给了我一个错误,指出无效的列名Department_id。...并且在生成的查询中也使用了department_id。当我只是通过部门循环和输出部门名称,它工作正常。

任何想法是什么我想丢失的部门ID正确的列名称?这里是我的模型对象,以防万一:

  public class Department 
{
public virtual int DepartmentID {得到;组; }
公共虚拟字符串DepartmentName {get;组; }

公共虚拟ICollection<员工>员工{get;组; }
}

public class Employee:PersistentEntity
{
public virtual int EmployeeID {get;组; }
public virtual string UserName {get;组; }

公共虚拟部门{get;组; }
}


解决方案

一个流利的NHibernate 约定,以便将 HasMany 外键创建为<'Name'> ID



或更改部门映射:

  HasMany(m => m.Employees).KeyColumns.Add(DepartmentID)


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; }
}

解决方案

You may either: create a Fluent NHibernate convention so that the HasMany "foreign key" is created as <'Name'>ID.

Or change the Department mapping:

 HasMany(m => m.Employees).KeyColumns.Add("DepartmentID")

这篇关于流利的NHibernate的HasMany外键映射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆