1

我有以下类和相关的映射(Fluent NHibernate):

public class Category
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual Category ParentCategory { get; set; }
    public virtual IList<Category> ChildCategories { get; set; }
}

映射:

public class CategoryMap : ClassMap<Category>
{
    public CategoryMap()
    {
        Table("Categories");

        Id(x => x.Id).Column("Id").CustomType("Int32").Access.Property()
         .CustomSqlType("int").Not.Nullable().Precision(10)
         .GeneratedBy.Identity();

        Map(x => x.Description).Column("Description").Nullable()
         .Generated.Never().CustomType(typeof (string)).Access
         .Property().Length(250);

        Map(x => x.Name).Not.Nullable().Generated.Never().CustomType("string")
         .Access.Property().Column("Name").Length(50);

        References(x => x.ParentCategory).Column("ParentCategoryId");
        HasMany(x => x.ChildCategories).KeyColumn("ParentCategoryId").Inverse()
         .AsBag().Fetch.Select();
    }
}

我创建两个Category对象如下:

var c = new Category
        {
            Name = "Ebooks",
            Description = "Contains awz, mobi, pdf, epub and other 
                electronic books"
        };
var cc = new Category
         {
            Name = "Kindle Books",
            Description = "Contains Kindle ebook reader format books 
                    (awz, mobi)",
            ParentCategory = c
         };
session.SaveOrUpdate(c);
session.SaveOrUpdate(cc);

当我尝试访问保存的对象时:

var c = session.Load<Category>(1);
var cc = c.ChildCategories;

c包含Category对象Id为 1 但其ChildCategories属性为null.

我做错了什么?

PS:-这是我的第一次实验,NHibernate等等Fluent NHibernate

编辑:-以下内容有效。我需要先关闭会话,然后再打开它进行阅读。否则,它只是从内存中读取,正如@Holf 指出我需要添加Child CategoryCategory那样:

c.ChilCategories.Add(cc);

我只是做了如下:

var session = sf.OpenSession();
CreateCategory(session);//ADDED TWO CATEGORIES EBooks, Kindle Ebooks
session.Close();

session = sf.OpenSession();
FetchCategories(session);//READ CATEGORY EBooks AND ASSOCIATED CHILDREN
session.Close();
4

2 回答 2

0

尽管您已经处理了关系的一侧,但通过将“c”指定为“cc”的 ParentCategory,我看不到您在任何地方都做了相反的事情。我想你也需要做

c.ChildCategories.Add(cc);

在执行 SaveOrUpdate 之前。

于 2012-05-07T19:35:36.437 回答
0

您能否将地图更新为以下内容:

public class CategoryMap : ClassMap<Category>
{
    public CategoryMap()
    {
        Table("Categories");

        Id(x => x.Id).Column("Id").CustomType("Int32").Access.Property()
         .CustomSqlType("int").Not.Nullable().Precision(10)
         .GeneratedBy.Identity();

        Map(x => x.Description).Column("Description").Nullable()
         .Generated.Never().CustomType(typeof (string)).Access
         .Property().Length(250);

        Map(x => x.Name).Not.Nullable().Generated.Never().CustomType("string")
         .Access.Property().Column("Name").Length(50);

        References(x => x.ParentCategory).Column("ParentCategoryId");
        HasMany(x => x.ChildCategories).KeyColumn("ParentCategoryId").Inverse()
         .Cascade.All();
    }
}

此外,您不能确定Idwith 值1是指父类别,请使用 LInq 提供程序加载正确的对象。

于 2012-05-08T02:52:25.027 回答