How to configure multiple mappings using FluentHibernate?

Posted by chris.baglieri on Stack Overflow See other posts from Stack Overflow or by chris.baglieri
Published on 2010-03-25T14:54:54Z Indexed on 2010/03/25 16:43 UTC
Read the original article Hit count: 165

First time rocking it with NHibernate/Fluent so apologies in advance if this is a naive question. I have a set of Models I want to map. When I create my session factory I'm trying to do all mappings at once. I am not using auto-mapping (though I may if what I am trying to do ends up being more painful than it ought to be). The problem I am running into is that it seems only the top map is taking. Given the code snippet below and running a unit test that attempts to save 'bar', it fails and checking the logs I see NHibernate is trying to save a bar entity to the foo table. While I suspect it's my mappings it could be something else that I am simply overlooking.

Code that creates the session factory (note I've also tried separate calls into .Mappings):

Fluently.Configure().Database(MsSqlConfiguration.MsSql2008
    .ConnectionString(c => c
        .Server(@"localhost\SQLEXPRESS")
        .Database("foo")
        .Username("foo")
        .Password("foo")))
    .Mappings(m => 
        { 
            m.FluentMappings.AddFromAssemblyOf<FooMap>()
                .Conventions.Add(FluentNHibernate.Conventions.Helpers
                .Table.Is(x => "foos"));
            m.FluentMappings.AddFromAssemblyOf<BarMap>()
                .Conventions.Add(FluentNHibernate.Conventions.Helpers
                .Table.Is(x => "bars"));
        })
    .BuildSessionFactory();

Unit test snippet:

using (var session = Data.SessionHelper.SessionFactory.OpenSession()) {      
   var bar = new Bar();
   session.Save(bar);
   Assert.NotNull(bar.Id);
}

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about fluent-nhibernate