【问题标题】:NHibernate: unexpected behavior when querying for an unmapped entityNHibernate:查询未映射实体时的意外行为
【发布时间】:2012-12-19 18:58:58
【问题描述】:

我有一个通用的 Repository 类来包装 Nhibernate 会话:

public class Repository<T>{

public IQueryable<T> GetAll(){

    return this.Session.Query<T>();
}

}

尽管我在底层数据库表中有数据映射到实体Foo,但调用Repository.GetAll&lt;Foo&gt;() 返回了一个空的可枚举。经过一番惊愕之后,我发现问题在于我忘记将我的类映射加载到 SessionFactory 中。

我很惊讶 NHibernate 在尝试加载没有映射的实体类型时不会引发异常。这感觉就像正是那种场景,应该会导致 NH 提前大声失败。

这是引导 NH 的代码。注意加载映射的两行被注释掉了

 private static Configuration BuildNHibernateConfig(Action<IDbIntegrationConfigurationProperties> dbIntegration)
        {
            var configuration = new Configuration();

            configuration
                .Proxy(p => p.ProxyFactoryFactory<DefaultProxyFactoryFactory>())
                .DataBaseIntegration(db =>
                {

                    db.ConnectionString = connectionString.Value;
                    db.Dialect<MsSql2008Dialect>();

                })
                .AddAssembly(typeof(ActionConfirmation<>).Assembly)
                .SetProperty("show_sql", "true")

                .CurrentSessionContext<LazySessionContext>();

         //   var mappings = GetMappings();
         //   configuration.AddDeserializedMapping(mappings, "Hydra");

            return configuration;
        }

        private static HbmMapping GetMappings()
        {
            var mapper = new ModelMapper();
            mapper.AddMapping<FacilityMap>();
            return mapper.CompileMappingForAllExplicitlyAddedEntities();

        }

这是正常行为还是我以某种方式错误配置了 NH?有没有办法覆盖这种行为?

【问题讨论】:

    标签: nhibernate exception


    【解决方案1】:

    LINQ 实现是 Criteria API 和此问题 has been reported 的包装器,并标记为“不会修复”。 Fabio Maulo 的评论表明,验证类是否已映射对性能影响太大。

    这对我来说也是新闻,我起初认为这是由于 LINQ 延迟执行。

    【讨论】:

    • NH-LINQ WAS 是标准 API 的包装器。从 NH 3.0 开始,LINQ 提供程序基于 HQL 引擎。
    • 另一点是可以这样做,例如 Query() 并且查询将包括所有映射的子类。
    猜你喜欢
    • 2011-09-28
    • 2011-07-07
    • 2013-01-22
    • 1970-01-01
    • 2011-10-06
    • 2014-03-04
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多