【发布时间】:2012-12-19 18:58:58
【问题描述】:
我有一个通用的 Repository 类来包装 Nhibernate 会话:
public class Repository<T>{
public IQueryable<T> GetAll(){
return this.Session.Query<T>();
}
}
尽管我在底层数据库表中有数据映射到实体Foo,但调用Repository.GetAll<Foo>() 返回了一个空的可枚举。经过一番惊愕之后,我发现问题在于我忘记将我的类映射加载到 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