【发布时间】:2014-05-11 15:46:15
【问题描述】:
我正在努力使用 NHibernate。我有一个小的 ASP MVC 网站。我设法设置了 NHibernate,但是当我想检索数据时,我收到了这个错误:
附加信息:无法初始化集合:[Lore.Models.DatabaseModels.Statue.Keywords#1] [SQL:选择keywords0_.IdStatueKeyword 作为IdStatue1_1_,keywords0_.IdKeyword 作为IdKeyword1_,keyword1_.IdKeyword 作为IdKeyword4_0_,keyword1_.Name 作为Name4_0_, 关键字1_.描述为Descript8_4_0_ FROM StatueKeyword 关键字0_ 左外连接 Statuekeyword1_ 在关键字0_.IdKeyword=keyword1_.IdKeyword WHERE 关键字0_.IdStatueKeyword=?]
我也不确定我是否正确实施了多对多关系。这是我的表结构:
雕像
- IdStatue
- 姓名
关键字
- IdKeyword
- 姓名
StatueKeyword
- IdStatueKeyword
- IdStatue
- IdKeyword
雕像类:
public class Statue
{
public Statue()
{
Keywords = new List<Keyword>();
}
public int IdStatue { get; set; }
public string Name { get; set; }
public IList<Keyword> Keywords { get; set; }
}
关键字类
public class Keyword
{
public int IdKeyword { get; set; }
public string Name { get; set; }
}
雕像 hbm 文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Lore.Models.DatabaseModels" assembly="Lore">
<class name="Statue" table="Statue" lazy="false" >
<id name="IdStatue" column="IdStatue">
<generator class="identity" />
</id>
<property name="Name" column="Name" not-null="true" type="System.String" />
<bag name="Keywords" table="StatueKeyword" lazy="false">
<key column="IdStatueKeyword"/>
<many-to-many class="Keyword" column="IdKeyword"/>
</bag>
</class>
</hibernate-mapping>
关键字 hbm 文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Lore.Models.DatabaseModels" assembly="Lore">
<class name="Keyword" table="Statue" lazy="false" >
<id name="IdKeyword" column="IdKeyword">
<generator class="identity" />
</id>
<property name="Name" column="Name" not-null="true" type="System.String" />
<property name="Description" column="Description" not-null="true" type="System.String" />
</class>
</hibernate-mapping>
我需要为硕士论文制作这个网站,因此非常感谢任何帮助!
【问题讨论】:
标签: asp.net-mvc nhibernate many-to-many nhibernate-mapping