0
votes

I am using .net 4.5 with NHibernate 3.3.3.4000 and I want to configurate a nhibernate.config with 2 session-factories. I tried to configurate my nhibernate.config this way:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory name="Configuration">
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
    <property name="connection.connection_string">Data Source=Configuration.db;Version=3</property>
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
    <property name="query.substitutions">true=1;false=0</property>
    <property name="show_sql">false</property>
    <mapping assembly="DataModel"/>
  </session-factory>
  <session-factory name="Texts">
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
    <property name="connection.connection_string">Data Source=Texts.db;Version=3</property>
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
    <property name="query.substitutions">true=1;false=0</property>
    <property name="show_sql">false</property>
    <mapping assembly="DataModel_Texts"/>
  </session-factory>
</hibernate-configuration>

But I get a HibernateConfigException that the "session-factory" is invalid: An exception occurred parsing configuration :The element 'hibernate-configuration' in namespace 'urn:nhibernate-configuration-2.2' has invalid child element 'session-factory' in namespace 'urn:nhibernate-configuration-2.2'.

Do you have an idea how I can configure the file with 2 session factories?

best regards Phil

1
You should be using NH4 with .Net 4.5.Oskar Berggren
Is that really the only information you get in the exception AND all it's InnerExceptions, if any?Oskar Berggren
Maybe this article can help you: codeproject.com/Articles/14846/…Felipe Oriani
I added the inner exeption to the question. There is a problem with the name of the session-factory. If I delete the second session-factory it's working. Is it allowed to have a second session factory in the same nhibernate.config file? Or do I have to use another config file? I will also check the link of Felipe.Phil

1 Answers

0
votes

You might try NHibernate X-Factories which provides extension methods for the NHibernate Configure methods. The extension methods allow a name to be specified to select the appropriate session factory from the configuration file.

There is minimal setup, but your configuration file would look something like this:

<hibernate-configuration-x-factories xmlns="urn:nhibernate-configuration-2.2-x-factories">
  <session-factory name="Configuration">
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
    <property name="connection.connection_string">Data Source=Configuration.db;Version=3</property>
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
    <property name="query.substitutions">true=1;false=0</property>
    <property name="show_sql">false</property>
    <mapping assembly="DataModel"/>
  </session-factory>
  <session-factory name="Texts">
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
    <property name="connection.connection_string">Data Source=Texts.db;Version=3</property>
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
    <property name="query.substitutions">true=1;false=0</property>
    <property name="show_sql">false</property>
    <mapping assembly="DataModel_Texts"/>
  </session-factory>
</hibernate-configuration-x-factories>

Configuring the session factory could be done in the following way:

NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();

config
    .Configure(HostingEnvironment.MapPath("~/nhibernate.cfg.xml"), "Development")
    .BuildSessionFactory();

Documentation can be found on the github repo.