【问题标题】:map table to itself in Parent-Child relationship Fluent Nhibernate在父子关系 Fluent Nhibernate 中将表映射到自身
【发布时间】:2010-12-02 09:56:30
【问题描述】:

我有将表的列映射到同一个表的主键的情况。表格是这样的

---+-------+---------
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>

【问题讨论】:

  • 你有没有彻底修复过它?我也有同样的问题

标签: nhibernate fluent-nhibernate


【解决方案1】:

看来您应该在映射中使用HasMany 而不是References

【讨论】:

  • 我做了,请查看我编辑的帖子以了解它引起的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
  • 2011-04-10
  • 1970-01-01
相关资源
最近更新 更多