Fluently. 从配置文件配置 NHibernate 数据库?
这个问题类似于 从 NHibernate 配置部分配置 Fluent NHibernate 但我我仍在努力思考这个问题,但答案还不够。
我想制作一个与数据库无关的库存管理应用程序(更多的是作为一个实验而不是其他任何东西)。我想使用 Fluent 自动映射器,因为如果我能让它工作,这似乎是一个非常酷的想法......让事情变得美好和通用,并且有点强迫你使用模式。
因此,我使用 SessionFactory 创建者创建一个辅助类,如下所示:
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 是我以前从未使用过的技术,因此这是一个陡峭的学习曲线。
This question is similar to Configuring Fluent NHibernate from NHibernate config section but I'm still trying to wrap my brain around this and the answer is not sufficient.
I want to make a database-agnostic inventory management application (more as an experiment than anything else). I want to use the Fluent automapper because it seems like a really cool idea if I could get it to work... keeps things nice and generic and kinda forces you to use a pattern.
So, I make a helper class with a SessionFactory creator like so:
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();
}
I make an app.config like so:
<?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>
The error message I get when I try to generate a session to play with is as follows:
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.
There is no inner exception.
I would think this works, but I would, of course, be wrong. An examination of the config object shows that it has collected the property information in the app config, yet cannot seem to apply it to the fluent Database Method. I have seen examples very similar to this all over the internet, though none matched exactly, so there's lots of room for stupid mistakes.
I'm sure this is a simple thing, but I've been scratching my head over this for hours. Any help would be greatly appreciated. Nhibernate and FluentNhibernate are techs that I've never used before so this has been a steep learning curve.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的方法看起来不错; Fluent NHibernate 确实应该允许您从配置开始并向其添加。下面是来自 Fluent NHibernate 站点的代码,它在语义上与您的站点相同,只是您是自动映射而不是流畅映射:
我看到的唯一主要区别是该站点的代码从不显式地将 config 对象变量分配给 Configure( ) 方法;这意味着Configure()除了返回其自身(或仅返回其自身)的深层克隆之外,还修改了调用它的实例。
需要检查的一些事项:您确定您使用的 Fluent 版本引用了使用配置版本 2.2 的 NHibernate 版本吗? FNH 1.2 针对 NH 3.1,而 FNH 1.1 针对 NH 2.1.2GA。我不知道它们使用哪个配置版本,但如果它们与 NH 版本匹配,那么Configure()方法可能找不到与其使用的ConfigSectionHandler匹配的配置节。
Your method seems sound; Fluent NHibernate should indeed allow you to start with a Configuration and add to it. Here's the code from the Fluent NHibernate site, which is semantically identical to yours except you're automapping instead of fluently mapping:
The only major difference I see is that the site's code never explicitly assigns the config object variable to the results of the Configure() method; this would imply that Configure() modifies the instance on which it's called in addition to returning a deep clone of itself (or just itself).
Some things to check: Are you SURE that the version of Fluent you're using references a version of NHibernate that uses the configuration version 2.2? FNH 1.2 targets NH 3.1, while FNH 1.1 targets NH 2.1.2GA. I do not know which configuration versions either of those use, but if they match the NH version then the Configure() method may not be finding the config section matching the ConfigSectionHandler it's using.
我认为 Keith 是对的,因为您实际上并没有将 NHibernate 配置填充到 Fluently.Configure 中,因此您的配置基本上被忽略。我知道您可能希望将这些设置保留在您的配置文件中,但是如果您愿意绕过配置文件,您也可以这样做
,如果您这样做,您将不再需要
在您的配置文件中,它应该可以解决您的问题。I think that Keith is right in that you are not actually populating the NHibernate configuration you have into
Fluently.Configure
, and thus your configuration is basically ignored. I know you may wish to keep these settings in your configuration file, but if you are willing to bypass the configuration file, you can also doIf you do that, you no longer need
<hibernate-configuration>
in your configuration file, and it should fix your problem.嗯,感谢您的健全性检查。事实证明,问题与自动映射配置有关,与数据库完全无关。错误消息让我困惑并导致我看错了地方。我想我会在 Fluent 论坛上注明,在这种情况下会弹出错误的消息,这样其他人就不会在这个问题上浪费时间。
Well thanks for the sanity check. It turns out that the problem had to do with the automapping configuration, nothing to do with the database at all. The error message was throwing me and causing me to look in the wrong place. I think I will make a note on the Fluent forum that the wrong message is popping up in that scenario, so that other people don't end up spinning their wheels on this.