2
votes

I'm doing a project in Nhibernate with MySql in asp.net. In that while executing the code I got the error like

An exception occurred during configuration of persistence layer

in the below line

ISessionFactory factory = new NHibernate.Cfg.Configuration().Configure).BuildSessionFactory();

So let me help to trouble shoot the error.

Here s my Configuration file

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <!-- an ISessionFactory instance -->
  <session-factory>
    <!-- properties -->
    <property name="connection.provider">
      NHibernate.Connection.DriverConnectionProvider
    </property>
    <property name="connection.driver_class"> 
      NHibernate.Driver.MySqlDataDriver
    </property>
    <property name="connection.connection_string">
      Server=localhost;Database=hrms;User ID=test;Password=test;
    </property>
    <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
    <property name="show_mysql">true</property>
    <!-- mapping files -->
    <mapping resource="WebApp1.Job.hbm.xml" assembly="WebApp1" />
  </session-factory>
</hibernate-configuration>
3
You should post your configuration as well.Mattias Jakobsson
That error is quite generic and could be caused by numerous factors but one possible cause could be a missing or malformed hibernate.cfg.xml.Darin Dimitrov

3 Answers

2
votes

Incomplete configuration perhaps? Try manual configuration initialization like the following:

NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.SetProperty("dialect", "NHibernate.Dialect.MySQLDialect");
cfg.SetProperty("connection.driver_class", "NHibernate.Driver.MySqlDataDriver");
cfg.SetProperty("connection.connection_string", "Server=YourServer;Database=YourDatabase;User ID=YourId;Password=YourPass;CharSet=utf8");
cfg.SetProperty("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu");  
cfg.AddAssembly("Your.Assembly.Name");  
ISessionFactory sessionFactory = cfg.BuildSessionFactory();

If everything works, move it to XML if you like.

1
votes

Please read the inner exception that is being thrown and it's very likely you would know the cause. In my experience it can be as simple as the code is looking for the hibernate.cfg.xml file in bin/debug and could not find it.

0
votes

I had a similar problem. Problem was that I used in Web.config:

<section name="nhibernate" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<nhibernate xmlns="urn:nhibernate-configuration-2.2">
    .
    .
    .
</nhibernate>

instead of:

<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    .
    .
    .
</hibernate-configuration>