Fluent / NHibernate 同类集合

发布于 2024-08-25 05:03:26 字数 960 浏览 5 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靖瑶 2024-09-01 05:03:26

按照惯例,Fluent NHibernate 会将“Category_Id”视为外键列。它不会计算出您的“ParentCategoryId”列。除非您将自引用列重命名为“Category_Id”,否则您必须澄清父子关系的列名称。

对于没有父级的类别(绝对父级类别),其引用列为 null,由于您选择了预加载,因此根据 NHibernate 如何处理它,将其自身返回为父级或 null 是合理的。

public class CategoryMap : ClassMap<Category> 
{ 
    public CategoryMap() 
    { 
        Id(x => x.Id); 
        Map(x => x.Name); 

        References(x => x.ParentCategory)
            .Column("ParentCategoryId")    // Many-To-One : parent
            .Nullable() 
            .Not.LazyLoad(); 

        HasMany(x => x.SubCategories) 
           .Cascade.All().Inverse().KeyColumn("ParentCategoryId");   //One-To-Many : chidren

    } 
} 

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.

public class CategoryMap : ClassMap<Category> 
{ 
    public CategoryMap() 
    { 
        Id(x => x.Id); 
        Map(x => x.Name); 

        References(x => x.ParentCategory)
            .Column("ParentCategoryId")    // Many-To-One : parent
            .Nullable() 
            .Not.LazyLoad(); 

        HasMany(x => x.SubCategories) 
           .Cascade.All().Inverse().KeyColumn("ParentCategoryId");   //One-To-Many : chidren

    } 
} 
秋风の叶未落 2024-09-01 05:03:26

好的,在 HasMany(x=>x.SubCategories) 上,您需要将 Inverse() 添加到调用链中,并为其提供我所指定的列名称假设给定父类别的映射是“ParentCategoryId”,当然这也取决于您的约定。

如果您要发布您的表格结构,我可以为您提供完整的解决方案。

Ok so on the HasMany(x=>x.SubCategories) you need to add Inverse() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文