An exception occurred during configuration of persistence layer - c#

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>

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.

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.

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>

Related

NHibernate throws an exception 'Data Source cannot be empty'?

I'm a newbie to NHibernate and trying to get my first app working with NHibernate + SQLite - In memory DB.
so far, I've done the following,
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<reflection-optimizer use="true"/>
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
<property name="connection.connection_string">"Data Source=:memory:;Version=3;New=True;"</property>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="connection.release_mode">on_close</property>
<property name="query.substitutions">true=1;false=0</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
Hibernate.cs
public NHibernate.ISessionFactory Hibernate()
{
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"));
cfg.AddAssembly(System.Reflection.Assembly.GetExecutingAssembly());
var sessionFactory = cfg.BuildSessionFactory();
return sessionFactory;
}
Now when I call Hibernate() function, the following line throws an exception,
var sessionFactory = cfg.BuildSessionFactory();
Exception :
System.ArgumentException: 'Data Source cannot be empty. Use :memory: to open an in-memory database'
I've been trying to fix this for quite some time now, I have tried with different connection strings, but I've had no luck. Any suggestions/guidance would be much appreciated.
Many thanks.
I doubt there should be double quotes around the value of the connection string property in the configuration file.

NHibernate error: Severity Code Description Project File Line Suppression State Error Unrecognized configuration section session-factory

I am trying to connect my aplication to a sql server 2012 database, but its not working and its giving the error that appears on the title, someone could help me? here is the code:
`
<?xml version="1.0"?>
<configuration>
<!-- an ISessionFactory instance -->
<session-factory>
<!-- properties -->
<property name="connection.connection_string">
User ID=****;Password=****;Data Source=****;Initial Catalog=****;
</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
<!-- mapping files -->
<mapping resource="NHibernate.Auction.Item.hbm.xml" assembly="NHibernate.Auction"/>
<mapping resource="NHibernate.Auction.Bid.hbm.xml" assembly="NHibernate.Auction"/>
</session-factory>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration>
`
Check the doc for more configuration examples
Chapter 23. Example: Weblog Application
here is a snippet of such setting (check the <configSections>):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Add this element -->
<configSections>
<section
name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<!-- Add this element -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>

NHibernate 2.1.2 - how to disable reflection optimizer

I want to disable reflection optimization (testing purposes), but i don't know where to do it. NH 2.1.2 uses hibernate-configuration in XML, and docs clearly state that this setting can not be set here. :/ I tried doing it the old App.config way with key/value pairs, no luck ...
Also, did NH 2+ version change something about reflection optimization?
Did you try
<add key="hibernate.use_reflection_optimizer" value="false" />
?
From the Hibernate Community:
I was able to set the
hibernate.use_reflection_optimizer
property in the web.config file as
follows. Note that the setting did not
work within the
hibernate-configuration section, so I
had to place it in a new nhibernate
section. The code now appears to be
working in a medium trust environment
( godaddy )
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
requirePermission="false"/>
<section name="nhibernate"
type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MySQLDialect</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">blahblah</property>
</session-factory>
</hibernate-configuration>
<nhibernate>
<add key="hibernate.use_reflection_optimizer" value="false" />
</nhibernate>

Configuring NHibernate via Web.config in ASP.NET 4.0

So my unit tests are green, time to integrate this shiny new NHibernate-driven DAL in to my web app! I don't really want to maintain two configuration files so I've migrated hibernate.cfg.xml in to my Web.config file (i.e. I copypasta'd the contents of hibernate.cfg.xml in to my Web.config). Here is the relevant bits from my Web.config:
<configSections>
<section name="combres" type="Combres.ConfigSectionSetting, Combres, Version=2.0.0.0, Culture=neutral, PublicKeyToken=49212d24adfbe4b4"/>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<nhibernate xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="">
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=(local)\SQLExpress;Initial Catalog=MyProject;Integrated Security=True</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<listener class="MyProject.Sql.Listeners.SaveEventListener, MyProject" type="save"/>
<listener class="MyProject.Sql.Listeners.UpdateEventListener, MyProject" type="update"/>
</session-factory>
</nhibernate>
In Global.asax, on Application_Start, I try to initialize my configuration:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
SessionProvider.Initialize();
}
All this really does is call new Configuration().Configure().AddAssembly("MyProject"); in accordance with the configuration code above.
Interesting result: When I first hit the page, an exception is thrown:
[FileNotFoundException: Could not find file 'D:\Build\MyProject\Source\MyProject.Web\bin\hibernate.cfg.xml'.]
Well, I put the configuration in Web.config, shouldn't it be lookign there? Do I need to indicate "hey, NHibernate, pay attention -- the config data is in Web.config, dummy!" anywhere?
When I then hit F5, the page comes up. Hurray! Now I try to do something with data access and I get this exception:
[ProxyFactoryFactoryNotConfiguredException: The ProxyFactoryFactory was not configured.
Initialize 'proxyfactory.factory_class' property of the session-factory configuration section with one of the available NHibernate.ByteCode providers.
Example:
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
Example:
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>]
Huh, that's kinda weird too -- this worked just fine in test with configuration in hibernate.cfg.xml...and I am specifying this property in my Web.config...I wonder what could possibly be up?
So, anyone have any ideas? Any help in solving this mystery would be super!
*Update: I found the issue. It looks like I wasn't using the correct type in my configs section! D'oh. I have a complete write up on my blog.
Try calling the .Configure() method at the end:
new Configuration().AddAssembly("MyProject").Configure();
Or if you prefer put it into the web.config:
<nhibernate xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="">
...
<mapping assembly="MyProject" />
</session-factory>
</nhibernate>
and then:
new Configuration().Configure();
Also make sure that the NHibernate.ByteCode.Castle.dll assembly is referenced in your web project.
It turns out that I was using the wrong type in the configuration section. You need to use NHibernate's section handler, not the generic .NET one. The behavior I was seeing was because it was all loaded in a singleton. On first visit, the configuration would fail. On subsequent visits it would just throw weird errors because the configuration failed originally!
There is one other caveat -- I have a complete writeup on my blog.

NHibernate - No Class Mappings

Why don't I have any class mappings after calling Configuration.Configure()?
Here is my class mapping file Category.hbm.xml for BudgetModel.Category:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="BudgetModel" namespace="BudgetModel">
<class name="Category" table="Categories">
<id name="Id" type="Int32">
<generator class="native" />
</id>
<property name="Name" type="string" not-null="true" />
</class>
</hibernate-mapping>
EDIT
NH version is 2.1.1.GA
Category.hbm.xml is an embedded resource & I have rebuilt.
You need to tell NHibernate where your mapping files are. You normally do this either programmatically or in the configuration file.
config.AddAssembly(typeof(Category).Assembly);
or
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<!--Configuration Properties-->
<mapping assembly="BudgetModel" />
</session-factory>
</hibernate-configuration>
Also, your hibernate mapping file must be set with a build action of embedded resource.

Categories

Resources