这个问题与Configuring Fluent NHibernate from NHibernate config section类似,但我仍然试图将我的大脑包围起来并且答案是不够的。
我想创建一个与数据库无关的库存管理应用程序(更像是一个实验而不是其他任何东西)。我想使用Fluent自动化器,因为如果我可以让它工作,这似乎是一个非常酷的想法......保持良好和通用,并且有点迫使你使用模式。
所以,我用SessionFactory创建者创建一个helper类,如下所示:
private static ISessionFactory _SessionFactory;
public static ISessionFactory SessionFactory {
get {
if (_SessionFactory == null) {
var config = new Configuration().Configure();
_SessionFactory = Fluently.Configure(config)
.Mappings (m => m.AutoMappings.Add (AutoMap.AssemblyOf<Machine> ()))
.BuildSessionFactory ();
}
return _SessionFactory;
}
}
public static ISession GetSession()
{
return SessionFactory.OpenSession();
}
我像这样制作一个app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<connectionStrings>
<add name="NHDataModel" connectionString="Data Source=10.10.10.10;Integrated Security=SSPI;Database=Inventory;" />
</connectionStrings>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.connection_string_name">NHDataModel</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
</configuration>
我尝试生成要播放的会话时收到的错误消息如下:
An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
* Database was not configured through Database method.
没有内在的例外。
我认为这有效,但我当然会错。对配置对象的检查表明它已经在应用程序配置中收集了属性信息,但似乎无法将其应用于流畅的数据库方法。我在互联网上看到了与此类似的例子,虽然没有完全匹配,所以有很多愚蠢错误的空间。
我确信这是一件很简单的事情,但我已经在这几个小时里摸不着头脑了。任何帮助将不胜感激。 Nhibernate和FluentNhibernate是我以前从未使用过的技术,所以这是一个陡峭的学习曲线。
答案 0 :(得分:4)
你的方法看似合理;流畅的NHibernate确实应该允许您从配置开始并添加到它。这里是来自Fluent NHibernate站点的代码,它在语义上与你的相同,除了你自动化而不是流利的映射:
var cfg = new NHibernate.Cfg.Configuration();
cfg.Configure(); // read config default style
Fluently.Configure(cfg)
.Mappings(
m => m.FluentMappings.AddFromAssemblyOf<Entity>())
.BuildSessionFactory();
我看到的唯一主要区别是网站的代码从未明确地将配置对象变量分配给Configure()方法的结果;这意味着除了返回自身的深度克隆(或仅仅是自身)之外,Configure()还会修改调用它的实例。
要检查的一些事项:您是否确定您使用的Fluent版本引用了使用配置版本2.2的NHibernate版本? FNH 1.2针对NH 3.1,而FNH 1.1针对NH 2.1.2GA。我不知道它们使用哪个配置版本,但如果它们与NH版本匹配,那么Configure()方法可能找不到与它正在使用的ConfigSectionHandler匹配的配置部分。
答案 1 :(得分:2)
我认为Keith是正确的,因为你实际上并没有将你所拥有的NHibernate配置填充到Fluently.Configure
中,因此你的配置基本上被忽略了。我知道你可能希望在配置文件中保留这些设置,但如果你愿意绕过配置文件,你也可以这样做
_SessionFactory = Fluently.Configure(config)
.Database(MsSqlConfiguration.MsSql2005.ConnectionString("the connection string"))
.Mappings (m => m.AutoMappings.Add (AutoMap.AssemblyOf<Machine> ()))
.BuildSessionFactory ();
如果您这样做,您的配置文件中不再需要<hibernate-configuration>
,它应该可以解决您的问题。
答案 2 :(得分:1)
非常感谢理智检查。事实证明,问题与自动化配置有关,完全与数据库无关。错误信息让我失望,导致我在错误的地方看。我想我会在Fluent论坛上做一个说明,在这种情况下会弹出错误的信息,这样其他人就不会最终在这上面转动。