7
votes

I am using Fluent NHibernate with an external 'hibernate.cfg.xml' file.

Following is the configuration code where I am getting error:

       var configuration = new Configuration();
       configuration.Configure();

       _sessionFactory = Fluently.Configure(configuration)
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Template>())
                .BuildSessionFactory();

        return _sessionFactory;

But When NHibernate is trying to configure, I am getting floowing error:

An exception occurred during configuration of persistence layer.

The inner exception says:

The ProxyFactoryFactory was not configured. Initialize 'proxyfactory.factory_class' property of the session-factory configuration section with one of the available NHibernate.ByteCode providers.

I googled and according to some solutions I found, I have made following changes:

  1. Add following dlls to my app bin:

    Castle.Core.dll, Castle.DynamicProxy2.dll, NHibernate.ByteCode.Castle.dll

  2. Added follwing property in hibernate.cfg.xml

    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>

But still I am getting the same exception.

10
If you click the exception helper in Visual Studio, you can see the InnerException (and possibly the InnerException after that). Can you look them up and include as much of the error information as possible? They probably reveal the actual problem.Abel

10 Answers

2
votes

The problem might be in your hibernate.cfg.xml, double check that is using 2.2 version and if well formed.

The mapping should start like this:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">

Along with the error you post, maybe more information is supplied because that error is quite generic to the configuration parser. If not, maybe you can give more details of your hibernate.cfg.xml.

14
votes

I had this error too. It fires when you don't copy the mapping file (hibernate.cfg.xml) to the build directory.

Solution:

  • In Solution explorer, right click on the mapping file (hibernate.cfg.xml), choose Properties, then make sure that Copy To Output Directory has Copy if newer selected).
5
votes

As Alex InTechno said, this might be the error in the definition of your Mapping files or in mapped entities. I experienced the same error, when I have forgot that all properties in mapped classes have to be defined as virtual.

public String Name {get;set;}
public virtual String Name { get; set; }
1
votes

Exception with the text of PotentialReasons

* Database was not configured through Database method.

might be also thrown by the FluentConfiguration.BuildConfiguration() method in the case your mappings are wrong (i.e. .*Map classes were not successfully parsed) and you have configured database not with .Database() method. This is a bit confusingly that it is discovered only at the step of building configuration, not when these classes are being added from assembly (by AddFromAssemblyOf<>)

You can check whether your *Map classes are successfully converted to HBM by executing a line m.FluentMappings.Add(typeof(YourMappedTypeMap)).ExportTo(@"c:\Temp\fluentmaps"))

1
votes

I had the same issue with NUnit tests under Resharper 8.1

Tick "ReSharper | Options | Tools | Unit Testing | Use separate AppDomain for each assembly with tests" checkbox fixed it

0
votes

Well, I was able to resolve that error by placing .cfg.xml file in bin of calling app.

But now, I am getting another error :-(

FluentNHibernate.Cfg.FluentConfigurationException was unhandled Message: 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.

Here is my 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="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Server=dev\sql2005;Initial Catalog=TestDB;Integrated Security=True</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration> 

Any thoughts?

0
votes

Trying to configure the database in App.config and the mappings via Fluent NHibernate, I also got a FluentNHibernateException saying

  • Database was not configured through Database method.

The link to show details was deactivated, so I couldn't get any further information. Searching for hours I finally found out that my connection string had an error ("pasword=xyz;" instead of "password=xyz;")

0
votes

Try to set modifier protected in set_id and set_sampelList. for example:

public virtual int Id {
    get; protected set;
}

and

public virtual IList<Store> StoresStockedIn {
    get; protected set;
}
0
votes

I begin receiving the

* Database was not configured through Database method.

error message while attempting to connect to a DB/2 database without having made any changes to my code. After verifying that the configuration file was being copied to the build directory and verifying that my XML was well-formed and conformed to version 2.2, I finally went to check the database to ensure that nothing had changed.

It turned out that the password for the connecting account had expired. It wasn't invalid nor was the account inactive--the password was merely expired. I am not sure why the expired password manifested in such an odd error message.

0
votes

It seems like this error can be a bunch of different things. I got the same error and eventually figured out it was because I had excluded an unused view and mapping from the project. No idea how this was causing the error but as soon as I added it back the error was gone.