Fluent NHibernate on PostgreSQL

When you write your first Fluent NHibernate application with Mono/.NET based on the Getting started tutorial, you eventually discover that you require a few extra assembly-dll references not mentioned. For my Postgres (PostgreSQL) project my references are:

Fluent NHibernate References

I won’t go into the detail of the matter, other than to say that many of these don’t give you a very clear indication as to what exactly is missing.

To configure Fluent NHibernate to work with Postgres you will need the following:

var connectionStr = "Server=127.0.0.1;Port=5432;Database=the_db;User Id=user_name;Password=password;"
ISessionFactory sessionFactory = Fluently
 .Configure()
 .Database(PostgreSQLConfiguration.Standard.ConnectionString(connectionStr))
 .Mappings(m => m.FluentMappings.AddFromAssemblyOf<TypeOfFluentNHibernateMapping>())
 .ExposeConfiguration(BuildSchema)
 .BuildSessionFactory();

private static void BuildSchema(Configuration config)
 {
// This NHibernate tool takes a configuration (with mapping info in)
// and exports a database schema from it.
var dbSchemaExport = new SchemaExport(config);
//dbSchemaExport.Drop(false, true);
dbSchemaExport.Create(false, true);
 }

TypeOfFluentNHibernateMapping is a class that inherits from FluentNHibernate.Mapping.ClassMap<T>. This tells Fluent to load all ClassMappings from the assembly where this type is defined.

BuildSchema(…) creates the database’s schema based on the specified mapping configuration and recreates the tables and the rest of it in the database specified by the connection string. I included the call to the schema export’s drop method, because the code originates from my unit tests, where I drop & recreate the database on each test run.

So far I like Fluent NHibernate, and the only complaint I have so far is the way NHibernate (not Fluent) handles enums. It assumes you want to use the enum member’s string name. The way I like to store my enums, are to have a separate table for them.

Advertisements

7 Comments on “Fluent NHibernate on PostgreSQL”

  1. Chris says:

    Could you contact me using my provided e-mail about this? I’d very much like to set up Fluent NHibernate on Mono 2.8 but am having some difficulties. Did you use Fluent NHibernate 1.1? Mono 2.8?

    • openlandscape says:

      Hi Chris,

      I’m more than willing to assist you here on this Blog post. I’m using Fluent NHibernate 1.1 and NHibernate 2.1.2.4 on Mono 2.8. What difficulties are you having? Are you getting any specific errors?

      • Chris says:

        There are two specific errors which may or may not be related. I should note that though your post is about PostgreSQL specifically, I am trying to use the Fluent NHibernate’s SQLite test at the moment. I should also note there appears to be a bug in Mono 2.8.2 which prevents one from using NHibernate which was not present in 2.8.1, so I am using 2.8.1.

        I’ve downloaded Fluent NHibernate 1.1 and opened the .sln in MonoDevelop. In an attempt to build the solution, building Fluent NHibernate itself seems to crash the compiler:

        Unhandled Exception: Mono.CSharp.InternalErrorException: /Users/cthielen/Downloads/jagregory-fluent-nhibernate-bd94ea5/src/FluentNHibernate/Mapping/ComponentMap.cs(25,18): FluentNHibernate.Mapping.ComponentMap —> Mono.CSharp.InternalErrorException: /Users/cthielen/Downloads/jagregory-fluent-nhibernate-bd94ea5/src/FluentNHibernate/Mapping/ComponentMap.cs(47,60): FluentNHibernate.Mapping.ComponentMap.Component(System.Linq.Expressions.Expression<System.Func>) —> System.ArgumentException: Invalid arguments
        at Mono.CSharp.TypeParameterInflator..ctor (Mono.CSharp.TypeSpec type, Mono.CSharp.TypeParameterSpec[] tparams, Mono.CSharp.TypeSpec[] targs) [0x00000] in :0
        at Mono.CSharp.Method.DefineTypeParameters () [0x00000] in :0
        at Mono.CSharp.Method.Define () [0x00000] in :0
        — snip —

        While that isn’t good, it’s not so worrisome as I can compile the Example project (http://github.com/jagregory/fluent-nhibernate/tree/master/src/Examples.FirstProject) and run it against the pre-compiled assemblies which come with the Fluent NHibernate download. However, upon running the program, it throws an exception:

        Missing method System.Data.Common.DbConnection::EnlistTransaction(Transaction) in assembly /Library/Frameworks/Mono.framework/Versions/2.8.1/lib/mono/gac/System.Data/4.0.0.0__b77a5c561934e089/System.Data.dll, referenced in assembly /Users/cthielen/Downloads/jagregory-fluent-nhibernate-bd94ea5/src/Examples.FirstProject/bin/Debug/System.Data.SQLite.dll

        Unhandled Exception: FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

        —> FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

        —> NHibernate.HibernateException: Method not found: ‘System.Data.Common.DbConnection.EnlistTransaction’. —> System.MissingMethodException: Method not found: ‘System.Data.Common.DbConnection.EnlistTransaction’.
        at NHibernate.Connection.DriverConnectionProvider.GetConnection () [0x00000] in :0
        — End of inner exception stack trace —

        — snip —

        Did you run into this? Could you compile the Example program and see what’s going on?

        My apologies as this is question is somewhat tangential from your original post.

      • Chris says:

        I think I may have figured out my problem. Using the .NET 3.5 runtime seems to get the Example working. The compiler still crashes while compiling Fluent NHibernate (I’m curious if you’re able to do that), but using the pre-compiled assemblies works.

        The .NET 4-causing issue seems related to the SQLite driver more so than anything else, so I expect when I follow this post to use PostgreSQL instead, it’ll work out.

  2. openlandscape says:

    Chris,

    I don’t remember each individual exception I got. I also didn’t bother with the example application and SQLite.

    For each library that didn’t have a Mono version, I compiled its source with MonoDevelop for Mono 2.8.0. I haven’t tested on Mono 2.8.1 or later.

    All my libraries are based on .NET 3.5. I also had problems mixing .NET 3.5 & 4 libraries, especially when my DI was .NET 3.5 and it was invoking .NET 4 objects.

    I tried to compile Fluent NHibernate with Mono 2.8 and it failed. It complains about ComponentMap with ArgumentException, invalid arguments.

    Have you tried using the Fluent NHibernate binaries?

    • Chris says:

      Yes, the binaries seem to be working and the bug in Mono preventing the compilation of FNH on 2.8.2 has been fixed in trunk. I think it’s all working well but it certainly wasn’t as smooth as I’d hoped. Thank you though – by the way, your post on this matter is one of the only I could find. Do blog about Mono in the future please!

      • openlandscape says:

        Cool. Glad you got it up and running eventually by using Fluent NHibernate’s binaries, instead of trying to build from source. My biggest complaint about Mono is not really about Mono itself, but rather the lack of support from other libraries and applications, like Fluent NHibernate. There are so many great libraries available for .NET, but a considerable number didn’t make sure it’s compatible with Mono, and hence using them is the equivalent of invading a samurai fortress protected with ninja bombs 🙂

        I have a number of plans for Mono & improving the Mono community, and blogging about is a bit part of that.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s