同一类的流利/ NHibernate的集合 [英] Fluent / NHibernate Collections of the same class

查看:148
本文介绍了同一类的流利/ NHibernate的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来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();

    }
}

每个类别可以有一个父类有些类别有很多子类,等等等等
我可以得到分类正确(正确子和父类FK存在于数据库中)保存,但加载时,自己返回作为父类。

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.

我使用流利的类映射,但如果有人可以点我在正确的方向只是简单的NHibernate,将正常工作。

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.

推荐答案

确定这样的的hasMany(X => x.SubCategories)你需要添加逆()来调用链,也给它这我假设是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.

如果你发布你的表stucture我可以给你一个完整的解决方案。

If you were to post your table stucture I can give you a complete solution.

这篇关于同一类的流利/ NHibernate的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆