Search code examples
nhibernatefluentautomapping

Fluent NHibernate: Mixing Automapping and manual mapping


If using Fluent NHibernate, is it possible to automap most classes, but specify that a couple of particular classes should be mapped using the regular fluent API rather than being automapped? And if so, can anyone point me to some sample code that shows how to do it?

Thanks!


Solution

  • It is possible and easy to mix-up mapping configurations:

    var cfg = Fluently.Configure()
        .Database(configurer)
        .Mappings(map =>
                      {
                          // Automapping
                          map.AutoMappings.Add(AutoMap.Assemblies(Assembly.GetExecutingAssembly())
                                                 .Where(type => type == typeof(Domain.Market.Share))
                                                 .Where(type => type == typeof(Domain.HR.Employee)));
    
                          // Fluent mappings
                          map.FluentMappings.AddFromAssemblyOf<Domain.Client.Macys>();
                      });
    

    Good luck. ;-)