Fluent NHibernate’s AutoPersistenceModel: I Love it!

by . 0 Comments

I checked out the latest version of Fluent NHibernate today and noticed the AutoPersistenceModel. I looked at the unit-tests to see what it’s supposed to do and I loved what I saw…

Let’s say you have a 2 simple classes like this:


Now, in the global.asax’s init (assuming a web application), we can configure NHibernate using Fluent NHibernate like this:


It uses convention over configuration in lots of places, eg the Id property is always used as identity-column if a property with that name exists.
But you could also easily add your own conventions. If you want to have the Id colums to be ArtistId and AlbumId (and any other className + Id), you’d do:


Want to restrict the namespace as well? No problem!


Fantastic! Now, to see what mapping file this code automagically creates for our Artist class, we can run the following code:


which returns an XmlDocument. In this case, the InnerXml of that document looks like this:


Again: fantastic! See how it even generates the one-to-many relation with the correct end-class, because we defined a property IList<Album>!

Running the same code for the Album class gives:


If you want to use Fluent NHibernate the ‘old’ way, you can still add ClassMap<Artist> to your assembly. Hence, you can even mix it with the AutoPersistenceModel! If you have a classmap file, it’ll use that for your class, if it isn’t there it’ll automatically create a mapping for you!

I’m sold! This makes using NHibernate a breeze. You can get up and running with your NHibernate project so much faster now.

0 Responses to Fluent NHibernate’s AutoPersistenceModel: I Love it!

  1. Reflective Perspective - Chris Alcock » The Morning Brew #237 says:

    […] Fluent NHibernate’s AutoPersistenceModel: I Love it! – Chris van de Steeg talks about Fluent NHibernate’s AutoPersistenceModel which takes the pain out of object relational mapping using NHibernate by generating the mappings for you based on common conventions – looks really useful for quick projects. […]

  2. znalec says:

    everybody knows that you loved what you saw by looking at the unit tests…
    but what about telling US what you saw? ;)

    not everyone on a plannet is a guru in nHibernate topic, and not everyone heard about Fluent mapping. I’ve used nHibernate a bit (and dropped it because of added complexity not needed in a web applications) and I’ve just heard about Fluent NHibernate but I really have no idea what your article was about, sorry…

    next time please try to compare 2 situations and clearly show what is the win of using the new feature for beginners, I I would be an expert in Fluent NHibernate I wouldn’t be in a need of reading your article in a first place…

    thanks for it anyway, at least I understand NHibernate isn’t as complex as it was…somehow ;)

    hope my _criticism_ will move your explanation to NHibernate beginners like me a little closer, thanks

  3. Jamie Ide says:

    Neat. I knew that auto mapping existed but I wasn’t sure how to customize it.

    By the way, you can use
    while initializing the configuration instead of loading it from configuration.

  4. Chris Marisic says:

    Could you please post your database schema you have your entities mapping to.

  5. Chris van de Steeg says:

    @ChrisMarisic: you can easily create your database schema like so:
    new NHibernate.Tool.hbm2ddl.SchemaExport(config).Execute(true, true, false, true);

  6. Peter Morris says:

    I’ve just come across another very similar blog on the same subject. I added the same comment to that blog too:

    It’s weird how this stuff is new. Back in 2002 I was using a tool that did this. In fact, not only does it automap + create the DB, you can also change your model and ask it to generate the script required to “evolve” the DB structure, which is useful at the point of release when you need a SQL script to upgrade a customer’s database structure as part of your server upgrade installer.

    It handles class renames, member renames, all sorts. I’ve linked it in the URL (which isn’t mine).

    I’ll take a look at NHibernate though, just out of curiousity.

  7. Pucca says:

    Hi, I tried using your code in my web application but I’m getting error bebow. Do you know what could possibly be causing this? thanks.
    If not, Can you post the code so I can compare it to my code and see what I’m doing wrong.

    Exception Details: System.InvalidOperationException: Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.

    Source Error:

    Line 55: var peristenceModel = AutoPersistenceModel.MapEntitiesFromAssemblyOf();
    Line 56:
    Line 57: peristenceModel.Configure(config);
    Line 58:
    Line 59: var document = peristenceModel

    Stack Trace:

    [InvalidOperationException: Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.]
    System.Reflection.RuntimeMethodInfo.ThrowNoInvokeException() +316
    System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +2753966
    System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +29
    System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +17
    FluentNHibernate.InvocationHelper.InvokeGenericMethodWithDynamicTypeArguments(T target, Expression`1 expression, Object[] methodArguments, Type[] typeArguments) +283
    FluentNHibernate.AutoMap.AutoPersistenceModel.FindMapping(Type type) +252
    FluentNHibernate.AutoMap.AutoPersistenceModel.Configure(Configuration configuration) +147

  8. Rusty says:

    Hello Chris, Would you happen to have Visual Studio 2005 solution build for this test, If so can you provide a zip file for it. I tried testing it, but keep getting null pointer excpetion.


  9. links for 2009-06-14 « Praveen’s Blog says:

    […] » Fluent NHibernate’s AutoPersistenceModel: I Love it! (tags: tools development .net c# configuration nhibernate fluent orm mapping fluentnhibernate) […]

  10. Roman says:

    I’ve dowloaded latest of Fluent and I see no AutoPersistenceModel.Map***() methods, neither Where(), neither WithCondition().
    Do I miss something crucial?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>