开发者

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

开发者 https://www.devze.com 2023-01-31 02:54 出处:网络
I have a application using NHibernate Auto Mapping... All working fine so far... My Fluent Global.asax config:

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>
  {
开发者_JAVA百科    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


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>();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消