【发布时间】:2013-02-19 00:56:40
【问题描述】:
我有 2 个具有循环依赖关系的表。一个Title 具有Elements,而每个Element 又指向路径中的下一个Title。
但是,当我在 NHibernate.Cfg.Configuration 的实例上调用 BuildSessionFactory() 时,我收到以下异常:
来自表 Element 的关联引用了一个未映射的类: ElementDAO
我相信这是循环依赖的结果(它不能先映射Title,因为它引用了Element,并且它不能在不知道Title的情况下映射Element)。
问题: 如何组合两个映射以解决循环依赖关系?
相关数据(和代码 sn-ps)如下:
标题
- Id(主键)
- 姓名
元素
- id_Title(外键)
- 姓名
- id_Title_Child(外键)
它们代表如下数据结构:
我的代码:
TitleDAO:
public class TitleDAO{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<ElementDAO> Elements { get; set; }
}
TitleDAO 映射:
<class name="TitleDAO" table="Title" lazy="true">
<id name="Id">
<generator class="identity"/>
</id>
<property name="Name"/>
<set name="Elements" table="Element" lazy="true">
<key column="id_Title"/>
<many-to-many class="ElementDAO"/>
</set>
</class>
ElementDAO:
public class ElementDAO{
public virtual string Name { get; set; }
public virtual TitleDAO Child { get; set; }
}
ElementDAO 映射:
<class name="ElementDAO" table="Element" lazy="true">
<property name="Name"/>
<many-to-one name="Child" class="TitleDAO" column="id_Title_Child"/>
</class>
【问题讨论】:
标签: c# nhibernate hbm