Fluent / NHibernate 同类集合
我是 NHibernate 的新手,在映射此类中的以下关系时遇到问题。
public class Category : IAuditable
{
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; }
public Category()
{
this.Name = string.Empty;
this.SubCategories = new List<Category>();
}
}
类映射(尽管,这些实际上是猜测)
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Id(x => x.Id);
Map(x => x.Name);
References(x => x.ParentCategory)
.Nullable()
.Not.LazyLoad();
HasMany(x => x.SubCategories)
.Cascade.All();
}
}
每个类别可能有一个父类别,某些类别有许多子类别,等等 我可以正确保存类别(数据库中存在正确的子类别和父类别 fk),但加载时,它返回自身作为父类别。
我使用 Fluent 进行类映射,但如果有人可以为我指明简单的 NHibernate 的正确方向,那也可以。
I am new to NHibernate and I am having trouble mapping the following relationships within this class.
public class Category : IAuditable
{
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; }
public Category()
{
this.Name = string.Empty;
this.SubCategories = new List<Category>();
}
}
Class Maps (although, these are practically guesses)
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Id(x => x.Id);
Map(x => x.Name);
References(x => x.ParentCategory)
.Nullable()
.Not.LazyLoad();
HasMany(x => x.SubCategories)
.Cascade.All();
}
}
Each Category may have a parent category, some Categories have many subCategories, etc, etc
I can get the Category to Save correctly (correct subcategories and parent category fk exist in the database) but when loading, it returns itself as the parent category.
I am using Fluent for the class mapping, but if someone could point me in the right direction for just plain NHibernate that would work as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按照惯例,Fluent NHibernate 会将“Category_Id”视为外键列。它不会计算出您的“ParentCategoryId”列。除非您将自引用列重命名为“Category_Id”,否则您必须澄清父子关系的列名称。
对于没有父级的类别(绝对父级类别),其引用列为 null,由于您选择了预加载,因此根据 NHibernate 如何处理它,将其自身返回为父级或 null 是合理的。
By convention, Fluent NHibernate will look at "Category_Id" as foreign key column. It won't figure out your "ParentCategoryId" column. Unless you rename your self-referencing column to "Category_Id", you have to clarify the column name for both parent and child relationships.
For category without parent (absolute-parent category), whose reference column is null, it's reasonable to return itself as parent or null depending on how NHibernate handles it since you choose eager loading.
好的,在
HasMany(x=>x.SubCategories)
上,您需要将Inverse()
添加到调用链中,并为其提供我所指定的列名称假设给定父类别的映射是“ParentCategoryId”,当然这也取决于您的约定。如果您要发布您的表格结构,我可以为您提供完整的解决方案。
Ok so on the
HasMany(x=>x.SubCategories)
you need to addInverse()
to the call chain and also give it the column name which I'm assuming is "ParentCategoryId" given the mapping of the parent category, of course this depends on your conventions too.If you were to post your table stucture I can give you a complete solution.