FluentNhibernate加入类

时间:2010-12-02 20:26:33

标签: nhibernate fluent-nhibernate

使用表定义:

Employee
     EmployeeId Primary Key
     LastName
     FirstName

EmployeeAddress
     EmployeeAddressId    
     EmployeeId
     Address
     City

的类定义
public class Employee
{
     public int EmployeeId {get; set;}
     public string LastName {get; set;}
     public string FirstName {get; set;}

     public EmployeeAddress EmployeeAddress {get; set;}
}

应该使用什么类型的映射来填充Employee.EmployeeAddress。我的第一次尝试是

Reference(x=>x.EmployeeAddress).Column("EmployeeId")

生成连接
Employee.EmployeeId = EmployeeAddress.EmployeeAddressId

我可以使用Join来做吗?像

这样的东西
Join("EmployeeAddress", join => join.Map(x => x.EmployeeAddress).  

调用configure时会生成错误。我似乎只能使用Join来获取个人属性,而不是类。如果我在地址和城市的Employee类中添加属性然后映射那些属性,我可以加入工作,但我想将它映射为一个类而不是每个单独的属性。

1 个答案:

答案 0 :(得分:0)

我不知道Fluent语法(对于one-to-one它是HasOne而对于many-to-one它是References),但这是生成的XML:

<class name="Employee">
  <id name="EmployeeId">
    <generator class="..." />
  </id>
  <property name="FirstName" />
  ...
  <one-to-one name="EmployeeAddress" cascade="all" property-ref="Employee" />
</class>
<class name="EmployeeAddress">
  <id name="EmployeeAddressId">
    <generator class="..." />
  </id>
  <property name="Address" />
  ...
  <many-to-one name="Employee" column="EmployeeId" unique="true" cascade="all" />
</class>