如何将 NHibernate 配置类与 Fluent NHibernate 一起使用

发布于 2024-08-11 07:09:17 字数 160 浏览 3 评论 0原文

即使在部署代码后我也希望保持灵活性,因此我喜欢使用 hibernate.cfg.xml 文件来配置 NHibernate。现在,我计划使用 Fluent NHibernate 来完成我所有的课程 =>表映射。有没有办法使用旧的 NHibernate 配置类来配置 Fluent NHibernate?

I want to be flexible even after deploying my code so I like to use the hibernate.cfg.xml file for configuring NHibernate. Now, I am planning to use Fluent NHibernate to do all my Class => Table mapping. Is there a way I could use the old NHibernate Configuration class to configure Fluent NHibernate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪化雨蝶 2024-08-18 07:09:17

是的,如果您使用 Fluent 配置 API,则 Configure 方法具有一个重载,它采用现有的 NHibernate Configuration 实例,该实例可以从 hibernate.cfg.xml 构建。

Yes, if you're using the fluent configuration API the Configure method has an overload that takes an existing NHibernate Configuration instance, which can be built from your hibernate.cfg.xml.

陌上青苔 2024-08-18 07:09:17

好吧,这显然是我的错。我尝试将 NHibernate Configurtion 对象传递到 Fluently.Configure() 方法中,但我的代码抛出了各种错误。问题出在 NHibernate“Fluent-NHibernate”用户的版本上。我不知道代理工厂类属性现在是强制性的。因此,我的 hibernate.cfg.xml 文件缺少该属性。这很奇怪,Fluent NHibernate 没有给我任何关于这一点的线索。当我尝试使用普通的 NHibernate 时,我发现了这个问题。以下是我的 hibernate.cfg.xml 文件的不同版本。希望它对未来的开发者有所帮助。

第一版

<?xml version="1.0" encoding="utf-8" ?>
<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,NHibernate</property>
    <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=SchoolPilot;Integrated Security=True</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

第二版

<?xml version="1.0" encoding="utf-8" ?>
<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,NHibernate</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=SchoolPilot;Integrated Security=True</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

Alright, So this was obviously my fault. I tried passing the NHibernate Configurtion object into the Fluently.Configure() method, but my code was throwing up all kinds of errors. The problem was with the version of NHibernate 'Fluent-NHibernate' users. I didn't know that the proxy factory class attribute was now mandatory. So, my hibernate.cfg.xml file was missing that attribute. It's wierd, Fluent NHibernate didn't give me any clue about that. It's when I tried using plain NHibernate that I found this problem. Below are the different version of my hibernate.cfg.xml files. Hope it helps future devs.

First Version

<?xml version="1.0" encoding="utf-8" ?>
<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,NHibernate</property>
    <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=SchoolPilot;Integrated Security=True</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

Second Version

<?xml version="1.0" encoding="utf-8" ?>
<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,NHibernate</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=SchoolPilot;Integrated Security=True</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文