在父子关系中将表映射到自身 Fluent Nhibernate
我遇到过将表的列映射到同一个表的主键的情况。该表看起来像这样
---+-------+---------
ID Name ParentId
---+-------+---------
1 Parent1 0
2 Child 1 1
3 Child 2 1
4 Parent2 0
5 Child 3 4
我创建了以下模型和 Fluent NHibernate 映射类
//Model.LocationType.cs
public class LocationType
{
public virtual long Id { get; set; }
public virtual string ShortName { get; set; }
public virtual string Description { get; set; }
public virtual IList<LocationType> ParentId { get; set; }
}
,
//Mapping.LocationTypeMap.cs
public class LocationTypeMap : ClassMap<LocationType>
{
public LocationTypeMap()
{
Table("SET_LOC_TYPE");
Id(x => x.Id).Column("LOC_TYPE_ID").GeneratedBy.Assigned();
Map(x => x.ShortName, "SHORT_NAME").Length(15).Not.Nullable();
Map(x => x.Description, "LOC_DESC").Length(50).Not.Nullable();
References(x => x.ParentId).Column("PARENT_LOC_TYPE_ID").Cascade.SaveUpdate();
}
}
但是当我执行代码时收到以下错误消息:
Unable to cast object of type 'SmartHRMS.Core.Domain.Model.LocationType' to type 'System.Collections.Generic.IList`1[SmartHRMS.Core.Domain.Model.LocationType]'.
编辑 1: 我没有尝试使用它,
HasMany(x => x.ParentIds).KeyColumn("PARENT_LOC_TYPE_ID");
虽然它有效并解决了我上面提到的铸造问题,但我得到的结果与我需要的相反。 在父对象的 LocationType 对象中,它列出了 IList 中的所有子对象,因此对于上面的示例,结果将是:
-----+----------+------
ID Name ParentId
-----+----------+------
1 Parent1 IList<Child1, Child2>
2 Child 2 IList<Empty>
3 .... same
4 Parent2 IList<Child3>
5 Child 3 IList<Empty>
I have situation where I am mapping table's columns to the primary key of same table. The table looks like this
---+-------+---------
ID Name ParentId
---+-------+---------
1 Parent1 0
2 Child 1 1
3 Child 2 1
4 Parent2 0
5 Child 3 4
I have created a following Model and Fluent NHibernate mapping class
//Model.LocationType.cs
public class LocationType
{
public virtual long Id { get; set; }
public virtual string ShortName { get; set; }
public virtual string Description { get; set; }
public virtual IList<LocationType> ParentId { get; set; }
}
and
//Mapping.LocationTypeMap.cs
public class LocationTypeMap : ClassMap<LocationType>
{
public LocationTypeMap()
{
Table("SET_LOC_TYPE");
Id(x => x.Id).Column("LOC_TYPE_ID").GeneratedBy.Assigned();
Map(x => x.ShortName, "SHORT_NAME").Length(15).Not.Nullable();
Map(x => x.Description, "LOC_DESC").Length(50).Not.Nullable();
References(x => x.ParentId).Column("PARENT_LOC_TYPE_ID").Cascade.SaveUpdate();
}
}
but I am receiving follow error message when i execute my code:
Unable to cast object of type 'SmartHRMS.Core.Domain.Model.LocationType' to type 'System.Collections.Generic.IList`1[SmartHRMS.Core.Domain.Model.LocationType]'.
Edit 1:
Instead of using i tried
HasMany(x => x.ParentIds).KeyColumn("PARENT_LOC_TYPE_ID");
Although it worked and solved the casting problem that I mentioned above but the result I am gettting is the reverse of what i need.
In parent's LocationType objects, it lists all childs in IList, so for above example the result will be:
-----+----------+------
ID Name ParentId
-----+----------+------
1 Parent1 IList<Child1, Child2>
2 Child 2 IList<Empty>
3 .... same
4 Parent2 IList<Child3>
5 Child 3 IList<Empty>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您应该在映射中使用
HasMany
而不是References
。Seems like you should use
HasMany
in your mapping instead ofReferences
.