NHibernate异常:无法初始化集合,列名无效。流利的映射。也许是多对一的问题? [英] NHibernate exception: could not initialize a collection, Invalid column name. Fluent mapping. Maybe a many-to-one issue?
问题描述
以下是事实:
- 我有一个
Person
类,它包含一个属性BillingManager
,它也是Person
类型。
- 我有一个
ExpenseReport
类,它包含一个属性SubmittedBy
,它是Person
类型。我将它映射为FNHReference。
- 我有一个
BillableTime
类,它包含一个属性Person
,它是Person
类型。
-
Person包含一个
类型(属性ExpenseReport
types(propertyExpenseReports
)
Person
包含code> BilledTime时间
)的集合(IList)
(见帖子底部的类和映射)
一直很酷,直到我添加了的IList< BilledTime>时间
集合到 Person
。现在,当我尝试访问 _person.Time
时,我得到一个异常:
代码:
//获得计费时间
if(_person.Time == null ||
_person.Time.Count x => x.Project.ProjectId == project.ProjectId)== 0)
{
//这个项目没有可结算时间
billableHours = Enumerable.Repeat(0F,14) .ToArray();
$ b $例外
$ b 无法初始化集合:
[MyApp.Business.Person.Time#211d3567-6e20-4220-a15c-74f8784fe47a]
[SQL:SELECT
time0_.BillingManager_id为BillingM8_1_,
time0_.Id为Id1_,
time0_.Id为Id1_0_,
time0_.ReadOnly为ReadOnly1_0_,
time0_.DailyHours为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映射中删除BillingManager引用(引用(p => p.BillingManager)
),异常消失,事情似乎工作(相对于BillableTime;它当然打破了BillingManager的持久性)。现在,似乎有一些自我引用的问题,因为 Person.BillingManager
属性本身是对 Person的引用
。
有什么想法发生在这里?我感到茫然...
谢谢。
===课程&映射===
public class Person
{
public virtual string LastName {get ;组; }
公共虚拟字符串名字{get;组; }
public virtual Person BillingManager {get;组; }
公共虚拟IList< ExpenseReport> ExpenseReports {get;组; }
公共虚拟IList< BillableTime>时间{get;组; }
}
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();
参考资料(p => p.BillingManager);
HasMany(p => p.ExpenseReports).Cascade.AllDeleteOrphan();
HasMany(p => p.Time).Cascade.AllDeleteOrphan();
public class BillableTime
{
public virtual int Id {get;私人设置}
public virtual Week Week {get;组; }
public virtual Person Person {get;组; }
public virtual Project Project {get;组; }
public virtual float [] DailyHours {get;组; }
公共虚拟发票发票{get;组; }
public virtual bool ReadOnly {get;组; }
}
public class BillableTimeMapping:ClassMap< BillableTime>
{
public BillableTimeMapping()
{
Id(x => x.Id);
参考文献(x => x.Week);
参考文献(x => x.Person);
References(x => x.Project);
参考文献(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;组; }
public virtual Person SubmittedBy {get;组; }
}
解决方案这个问题,但我不知道到底是为什么发生。如果我有空闲时间,我会进行调查。
HasMany(p => p.Time).Cascade.AllDeleteOrphan() .KeyColumn( PERSON_ID);
I am puzzled and frustrated by an exception I'm getting via NHibernate. I apologize for the length of this post, but I've tried to include an appropriate level of detail to explain the issue well enough to get some help!
Here's the facts:
- I have a
Person
class which contains a property BillingManager
, which is also a Person
type. I map this as an FNH "Reference".
- I have an
ExpenseReport
class which contains a property SubmittedBy
, which is a Person
type. I map this as an FNH "Reference".
- I have a
BillableTime
class which contains a property Person
, which is a Person
type. I map this as an FNH "Reference".
Person
contains a collection (IList) of ExpenseReport
types (property ExpenseReports
)
Person
contains a collection (IList) of BilledTime
types (property Time
)
(See classes and mappings at bottom of post.)
All was cool until I added the IList<BilledTime> Time
collection to Person
. Now, when I try to access _person.Time
, I get an exception:
The code:
// 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();
}
The exception:
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=?]
It's true that BillingManager_id
is an invalid column name, it doesn't exist in the BillableTime
table. However, I don't understand why NHB has created this SQL... doesn't make sense to me. I have seen this "Invalid column name" exception a lot when searching for a solution, but nothing seems to work. Even more confusing: like BilledTime
, the ExpenseReport
type also contains a reference to Person
and it works perfectly.
One thing I was able to figure out is that if I remove the BillingManager reference from the Person mapping (References(p => p.BillingManager)
), the exception goes away and things seem to work (with respect to BillableTime; it of course breaks the BillingManager persistence). Now it seems like there is some "self-reference" problem, since the Person.BillingManager
property is itself a reference to a Person
.
Any idea what is going on here? I'm at a loss...
Thanks.
=== Classes & Mappings ===
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; }
}
解决方案 the following line should solve the issue, but i' dont know exactly why it is happening. if i have the spare time i will investigate.
HasMany(p => p.Time).Cascade.AllDeleteOrphan().KeyColumn("Person_Id");
这篇关于NHibernate异常:无法初始化集合,列名无效。流利的映射。也许是多对一的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!