仅当我在我的 xml 文件中设置 property-ref 时,我才会收到此异常。
Initializing[Domain.Entities.R#12345] - 无法延迟初始化角色集合:Domain.Entities.R.LP,没有会话或会话已关闭
LP.hbm.xml
----------
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Domain.Entities" assembly="Domain">
<class name="LP" table="LP">
<id name="Id">
<column name="Id" sql-type="int" not-null="true"/>
</id>
<property name="AnotherField"/>
<property name="PaymentDate"/>
<property name="PaymentAmount"/>
</class>
</hibernate-mapping>
R.hbm.xml
---------
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Domain.Entities" assembly="Domain">
<class name="R" table="R">
<id name="Id">
<column name="Id" not-null="true"/>
</id>
<property name="AnotherField"/>
<set name="LP">
<key column="AnotherField" property-ref="AnotherField" />
<one-to-many class="Domain.Entities.LP" not-found="ignore" />
</set>
</class>
</hibernate-mapping>
IQueryable<Entities.R> query = _db.Query<Entities.R>();
var query2 = _db.Query<Entities.LP>().ToList();
var queryResults = query.ToList();
Iesi.Collections.Generic.ISet<Entities.LP> lp;
try
{
lp = queryResults.First().LP; <--- this fails with exception
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
var lp2 = _db.Query<Entities.LP>().Take(100); <-- works just fine
}
我不明白为什么 lp2 设置得很好,但 lp 失败了?我知道数据模型并不理想,但这是我现在必须使用的。如果我从 nhprof 中的 xml 文件中删除 property-ref,我会看到它调用 SQL 表(使用错误的值,所以我没有返回数据),但它没有失败。这只发生在我设置了 property-ref 时。
任何帮助将不胜感激。这是我第一次与 NH 合作。