Paul
Paul

Reputation: 12799

Choose between Auto Mapping and Fluent mapping with Fluent NHibernate (S#arp Architecture)

I have a application using NHibernate Auto Mapping... All working fine so far...

My Fluent Global.asax config:

private void InitializeNHibernateSession()
{
    NHibernateSession.Init(
         webSessionStorage,
         new string[] { Server.MapPath("~/bin/Proj.Data.dll") },
         new AutoPersistenceModelGenerator().Generate(),
         Server.MapPath("~/NHibernate.config"));
}

But I need to map a class with Fluent mapping... I created the class :

namespace Proj.Data.NHibernateMaps
{
  public class CategoryMap : IAutoMappingOverride<Category>
  {
    public void Override(AutoMapping<Category> mapping)
    {
        mapping.Id(x => x.Id)
            .GeneratedBy.Identity();

        mapping.Map(x => x.Description);  
        mapping.Map(x => x.UrlName);

        mapping.References(x => x.ParentCategory)
            .Not.LazyLoad();            
    }
  }
}

The problem is that this mapping is never used by the NHibernate... Instead it uses the Auto Mapping generated Category...

How can I use my Fluent Mapping ?

Thanks

Paul

Upvotes: 0

Views: 354

Answers (1)

Nick Patterson
Nick Patterson

Reputation: 944

Wherever you're configuring the AutoPersistenceModel you need to reference the mapping overrides. I find the easiest way to do this is to just point it at the assembly containing the mapping overrides and let it discover all of them. That way you can add new IAutoMappingOverride implementations and it will be automatically picked up. You do this using the UseOverridesFromAssemblyOf extension method.

public class AutoPersistenceModelGenerator {
    public AutoPersistenceModel Generate() {
        return AutoMap.AssemblyOf<Category>()
            .UseOverridesFromAssemblyOf<CategoryMap>();
    }
}

Upvotes: 4

Related Questions