在急切加载时通过grandparent属性对实体进行排序时出现Nhibernate异常

时间:2011-06-26 01:50:18

标签: nhibernate eager-loading

我有国家,州和城市实体的数据模型。每个城市实体都引用一个国家,每个国家都引用一个国家(UML diagram here)。这比我的实际数据模型简化得多。

对于UI中的城市列表,我正在使用Nhibernate从数据库中检索城市,我想(1)按国家名称(城市的祖父母实体)进行排序,以及(2)急切加载整体结构。我可以让NH做任何一件事,但不能两者都做。

我明白了:

  

查询指定的连接提取,但是提取的关联的所有者在选择列表中不存在[FromElement {显式,不是集合连接,获取连接,提取非延迟属性,classAlias = 2,role = ,tableName =“Country”,tableAlias = country4 ,origin =“State”state1_,colums = {state1_.Country_id,className = NHGrandparentSortingSpike.Entities.Country}}] [.ThenFetch [NHGrandparentSortingSpike.Entities.City,NHGrandparentSortingSpike .Entities.State,NHGrandparentSortingSpike.Entities.Country](取指[NHGrandparentSortingSpike.Entities.City,NHGrandparentSortingSpike.Entities.State](排序依据[NHGrandparentSortingSpike.Entities.City,System.String](NHibernate.Linq.NhQueryable`1 [ NHGrandparentSortingSpike.Entities.City],引用((c,)=>(c.State.Country.Name)),),引用((c,)=>(c.State)),),引用(( s,)=>(s.Country)),)]

实体:

    public class Country {
        public virtual int Id { get; private set; }
        public virtual string Name { get; set; }
        public virtual IList<State> States { get; set; }
        ...
    }
    public class State {
        public virtual int Id { get; private set; }
        public virtual string Name { get; set; }
        public virtual Country Country { get; set; }
        public virtual IList<City> Cities { get; set; }
        ...
    }
    public class City {
        public virtual int Id { get; private set; }
        public virtual string Name { get; set; }
        public virtual State State { get; set; }
    }

映射(使用Fluent NHibernate):

    public class CountryMap : ClassMap<Country> {
        public CountryMap() {
            Id(x => x.Id).Not.Nullable().GeneratedBy.Native();
            Map(x => x.Name).Not.Nullable().Length(100);
            HasMany(x => x.States).Inverse().Cascade.All();
        }
    }
    public class StateMap : ClassMap<State> {
        public StateMap() {
            Id(x => x.Id).Not.Nullable().GeneratedBy.Native();
            Map(x => x.Name).Not.Nullable().Length(100);
            References(x => x.Country).Not.Nullable();
            HasMany(x => x.Cities).Inverse().Cascade.All();
        }
    }
    public class CityMap : ClassMap<City> {
        public CityMap() {
            Id(x => x.Id).Not.Nullable().GeneratedBy.Native();
            Map(x => x.Name).Not.Nullable().Length(100);
            References(x => x.State).Not.Nullable();
        }
    }

查询: 延迟加载与排序(这按预期工作没有错误,但根据我的喜好发出太多SQL查询):

    var cities = _cityRepo.All.OrderBy(c => c.State.Country.Name);

急切加载但没有排序(这可以正常工作而没有错误,但没有排序,因此无法正确分页):

    var cities = _cityRepo.All.Fetch(c => c.State).ThenFetch(s => s.Country);

急切加载订购(这不起作用并抛出上面的错误):

    var cities = _cityRepo.All
                          .OrderBy(c => c.State.Country.Name)
                          .Fetch(c => c.State)
                          .ThenFetch(s => s.Country);

这是一个简单的VS2008控制台应用程序的zip文件,在内存数据库中使用SQLite来演示此问题: Download (2MB)

有什么想法吗?

感谢。

0 个答案:

没有答案