【问题标题】:Fluent NHibernate: Eager load multiple collections in mappingFluent NHibernate:在映射中急切加载多个集合
【发布时间】:2017-11-01 17:07:00
【问题描述】:

鉴于我有以下具有多个集合属性的实体...

public class Parent 
{
    public virtual int Id { get; set;}  
    public virtual ICollection<FirstChild> FirstChildren { get; set; }
    public virtual ICollection<SecondChild> SecondChildren { get; set; }
} 

有没有一种方法可以使用流畅的 NHibernate 同时预先加载这两个属性?或者只是急切地加载与父级关联的所有内容。

如果我有以下作为我的映射...

public ParentMapping()
{
    Id(p => p.Id).GeneratedBy.Identity();

    HasMany(p => p.FirstChildren)
        .Table("FirstChildren")
        .KeyColumn("Id")
        .Inverse()
        .Cascade.AllDeleteOrphan()
        .Fetch.Join();

    HasMany(p => p.SecondChildren)
        .Table("SecondChildren")
        .KeyColumn("Id")
        .Inverse()
        .Cascade.AllDeleteOrphan()
        .Fetch.Join();
}

上面的映射导致错误:

'不能同时提取多个包'。

如果我仅在其中一个属性上使用 Fetch.Join(),则在映射中使用它是有效的。

我可以通过使用 ToFuture() 查询来预先加载所有内容,但是,我更愿意在映射中执行此操作。

【问题讨论】:

    标签: c# nhibernate orm fluent-nhibernate


    【解决方案1】:

    您需要使用ISet 而不是ICollection 才能使用该功能。

    你可以看看herehere

    【讨论】:

    • 有没有不使用 ISet 的方法?好像我在其中一个集合属性上使用 Fetch.Join() 它工作正常。只有在多个属性上使用它时才会出现问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 2017-10-19
    • 1970-01-01
    • 2012-02-08
    相关资源
    最近更新 更多