我对通过 NHibernate 得到的异常感到困惑和沮丧。对于这篇文章的篇幅,我深表歉意,但我已尝试包含适当的详细信息以充分解释该问题以获得一些帮助!
以下是事实:
- 我有一个
Person
包含属性的类BillingManager
,它也是一种Person
类型。我将其映射为 FNH“参考”。 - 我有一个
ExpenseReport
包含属性的类SubmittedBy
,它是一种Person
类型。我将其映射为 FNH“参考”。 - 我有一个
BillableTime
包含属性的类Person
,它是一种Person
类型。我将其映射为 FNH“参考”。 Person
包含ExpenseReport
类型 (propertyExpenseReports
) 的集合 (IList)Person
包含BilledTime
类型 (propertyTime
) 的集合 (IList)
(请参阅帖子底部的类和映射。)
一切都很酷,直到我将IList<BilledTime> Time
集合添加到Person
. 现在,当我尝试访问时_person.Time
,出现异常:
编码:
// Get billable hours
if (_person.Time == null ||
_person.Time.Count(x => x.Project.ProjectId == project.ProjectId) == 0)
{
// No billable time for this project
billableHours = Enumerable.Repeat(0F, 14).ToArray();
}
例外:
could not initialize a collection:
[MyApp.Business.Person.Time#211d3567-6e20-4220-a15c-74f8784fe47a]
[SQL: SELECT
time0_.BillingManager_id as BillingM8_1_,
time0_.Id as Id1_,
time0_.Id as Id1_0_,
time0_.ReadOnly as ReadOnly1_0_,
time0_.DailyHours as DailyHours1_0_,
time0_.Week_id as Week4_1_0_,
time0_.Person_id as Person5_1_0_,
time0_.Project_id as Project6_1_0_,
time0_.Invoice_id as Invoice7_1_0_
FROM [BillableTime] time0_
WHERE time0_.BillingManager_id=?]
确实是无效的列名,它在表BillingManager_id
中不存在。BillableTime
但是,我不明白为什么 NHB 创建了这个 SQL……对我来说没有意义。在搜索解决方案时,我经常看到这个“无效的列名”异常,但似乎没有任何效果。更令人困惑的是:就像BilledTime
,该ExpenseReport
类型还包含对的引用Person
并且它工作得很好。
我能够弄清楚的一件事是,如果我从 Person 映射References(p => p.BillingManager)
(现在似乎存在一些“自我引用”问题,因为该Person.BillingManager
属性本身就是对 a 的引用Person
。
知道这里发生了什么吗?我很茫然...
谢谢。
=== 类和映射 ===
public class Person
{
public virtual string LastName { get; set; }
public virtual string FirstName { get; set; }
public virtual Person BillingManager { get; set; }
public virtual IList<ExpenseReport> ExpenseReports { get; set; }
public virtual IList<BillableTime> Time { get; set; }
}
public class PersonMapping : ClassMap<Person>
{
public PersonMapping()
{
Id(p => p.UserId).GeneratedBy.Assigned();
Map(p => p.LastName).Not.Nullable();
Map(p => p.FirstName).Not.Nullable();
References(p => p.BillingManager);
HasMany(p => p.ExpenseReports).Cascade.AllDeleteOrphan();
HasMany(p => p.Time).Cascade.AllDeleteOrphan();
}
}
public class BillableTime
{
public virtual int Id { get; private set; }
public virtual Week Week { get; set; }
public virtual Person Person { get; set; }
public virtual Project Project { get; set; }
public virtual float[] DailyHours { get; set; }
public virtual Invoice Invoice { get; set; }
public virtual bool ReadOnly { get; set; }
}
public class BillableTimeMapping : ClassMap<BillableTime>
{
public BillableTimeMapping()
{
Id(x => x.Id);
References(x => x.Week);
References(x => x.Person);
References(x => x.Project);
References(x => x.Invoice);
Map(x => x.ReadOnly).Not.Nullable().Default("0");
Map(x => x.DailyHours).Length(28);
}
}
public class ExpenseReport
{
public virtual long Id { get; set; }
public virtual Person SubmittedBy { get; set; }
}