流畅的NHibernate缺少一对多参考文献

时间:2014-02-12 21:27:40

标签: c# nhibernate fluent-nhibernate

我有抽象类MultipleItems,它有OrderItem对象列表:

public abstract class MultipleItems
{
    ...
    private IList<OrderItem> _orderItems = new List<OrderItem>();

    public virtual IList<OrderItem> OrderItems {
        get
        {
            return this._orderItems;
        }
        set { }
    }
    ...
}

Class Menu继承了MultipleItems类:

public class Menu : MultipleItems
{
    public virtual string Name { get; set; }

    public Menu(string name)
    {
        Name = name;
    }

    public Menu() { }
}

我按照每个类型映射它:

public class MultipleItemsMap : ClassMap<MultipleItems>
{
    public MultipleItemsMap()
    {
        Id(i => i.id);
        Map(i => i.Total);
        Map(i => i.Discount);
        HasMany(i => i.OrderItems).Cascade.All();
    }
}

public class MenuMap : SubclassMap<Menu>
{
    public MenuMap()
    {
        KeyColumn("id");       
        Map(i => i.Name);
    }
}

问题是,当我尝试加载菜单时,例如:

session.QueryOver<Menu>().Fetch(i => i.OrderItems).Eager.List();

列表是空的,它不应该......我也检查了SQL查询,它就在那里,但只是没有加载。

编辑:OrderItem类:

public class OrderItemMap : ClassMap<OrderItem>
{
    public OrderItemMap()
    {
        Id(x => x.id);           
        Map(x => x.Total);
        Map(x => x.Qty);
        Map(x => x.ItemName);
        Map(x => x.ItemType);
        References(x => x.Item).Not.LazyLoad();

    }
}

1 个答案:

答案 0 :(得分:0)

您对OrderItems的映射没有指定集合应该映射到私有字段,而空集导致列表不被填充。我会删除空的setter并将映射更改为:

public class MultipleItemsMap : ClassMap<MultipleItems>
{
    public MultipleItemsMap()
    {
        Id(i => i.id);
        Map(i => i.Total);
        Map(i => i.Discount);
        HasMany(i => i.OrderItems).Access.CamelCaseField(Prefix.Underscore)
            .Cascade.All();
    }
}