Monday, December 6, 2010

Using NHibernate 3 on Fluent NHibernate 2.0.0.967

If you are like most folks who want to experiment on latest and bleeding edge technology, encountering errors and making your way around it is the way of life. At the time of this writing, you will encounter this error...

cannot implicitly convert type 'NHibernate.ISessionFactory' to 'NHibernate.ISessionFactory'. An explicit conversion exists (are you missing a casts?)


...when you attempt to use Fluent NHibernate 2.0.0.967 on NHibernate 3. The code in question:

static ISessionFactory CreateSessionFactory()
{
 return Fluently.Configure()
  .Database
   (
    PostgreSQLConfiguration.Standard.ConnectionString("Server=localhost;Database=fluent_try;User ID=postgres;Password=xxxx;")
   )
  .Mappings( m => m.FluentMappings.AddFromAssemblyOf<MainClass>() )    
  .BuildSessionFactory();
}


Which seems odd, considering that BuildSessionFactory's method signature is:


public ISessionFactory BuildSessionFactory();
    


Though Fluent NHibernate 2.0.0.967 was built for NHibernate 2.1, the following would do the trick for convincing the compiler that NHibernate 3 is compatible with NHibernate 2.1:

static ISessionFactory CreateSessionFactory()
{
 return (ISessionFactory) Fluently.Configure() // note the explicit cast
  .Database
   (
    PostgreSQLConfiguration.Standard.ConnectionString("Server=localhost;Database=fluent_try;User ID=postgres;Password=xxxxxxxxx;")
   )
  .Mappings( m => m.FluentMappings.AddFromAssemblyOf<MainClass>() )    
  .BuildSessionFactory();
}


Happy Fluenting! :-)

EDIT: 2010-12-10 Apparently, the explicit cast trick works on Mono only, on Visual Studio, it will prompt you to add binding redirect records to app.config

UPDATE: 2010-12-13 There's now an NHibernate 3-compatible Fluent NHibernate: http://fluentnhibernate.org/dls/v1.x/fluentnhibernate-NH3.0-binary-1.2.0.694.zip

Here's the sample code: http://www.ienablemuch.com/2010/12/nhibernate-3-fluent-linq-one-stop-shop.html

No comments:

Post a Comment