【发布时间】:2012-05-14 00:05:15
【问题描述】:
我的“Category”对象有以下模型类:
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Category ParentCategory { get; set; }
public virtual IList<Category> SubCategories { get; set; }
这是我的“Category”流利的 nhibernate 映射类:
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Name);
References(x => x.ParentCategory).Column("ParentCategoryId");
// ** THE BELOW MAPPING IS WHAT I'M UNSURE ABOUT **
HasMany(x => x.SubCategories).Where(x => x.Id == x.ParentCategory.Id);
与此相关的我的数据库由许多“类别”组成,其中一些位于根级别(并且 ParentCategoryId = NULL),所有其他都是子类别,可能只有 1 级深,或者可能有 3、4、5 层(递归父级备份到根/父级 CategoryId。
行/记录之间的关系示例:
Cars (Id = 1 - ParentCategoryId = NULL)
Cars (Id = 1) > Hatchback (Id = 2 - ParentCategoryId = 1)
Cars (Id = 1) > Hatchback (Id = 2) > Ford (Id = 3 - ParentCategoryId = 2)
Motorcycles (Id = 4 - ParentCategoryId = NULL)
Motorcycles (Id = 4) > Scooters (Id = 5 - ParentCategoryId = 4)
我的 Category 类中的 'SubCategories' 属性需要检索所有具有当前 Category(Id) 的 'ParentCategoryId' 的类别,但我不确定我如何映射这个。我已经尝试了上面示例中显示的 HasMany 映射,但失败了。
【问题讨论】:
标签: fluent-nhibernate mapping nhibernate-mapping