一对一的一对多关系

时间:2012-10-11 03:06:51

标签: nhibernate

两个实体的映射文件如下,我想为两个实体构建ont-to-many关系但是“one”实体的列不是键列。因为DB表无法更改,我可以有一个方法建立它。请帮助我,thx。

<class name ="Sue" table="[Sue]">
    <id name="ID" column ="ID" type="Guid" />

    <property name="SueSmallType">
        <column name="SueSmallType" sql-type ="nvarchar(Max)" />
    </property>

</class>


<class name ="SueType" table="[SueType]">
    <id name="ID" column ="ID" type="Guid" />

    <property name="Code">
        <column name="Code" sql-type ="nvarchar(Max)" />
    </property>

</class>

例如建立与“SueSmallType”和“Code”的关系,我应该怎么做。

1 个答案:

答案 0 :(得分:1)

property-ref适用于这种情况,但是你会松开延迟加载,因为“SueSmallType”不是引用对象的Id。

<class name ="Sue" table="[Sue]">
    <id name="ID" column ="ID" type="Guid" />

    <many-to-one name="SueType" column="SueSmallType" property-ref="Code"/>
</class>

<class name ="SueType" table="[SueType]">
    <id name="ID" column ="ID" type="Guid" />

    <property name="Code">
        <column name="Code" length="8000" />
    </property>
</class>

请注意,certan阈值(例如8000)上的length属性与将sqltype设置为数据库长文本类型(nvarchar(max),text)具有相同的效果