Configuring Fluent NHibernate with an External NHibernate Config File


A while ago I was having issues using configuring fluent NHibernate with an external configuration file. I kept running into the issue of mappings not being registered. I was finally able to get it working appropriately and thought I would share my solution. The key is to configure the normal mappings first and then pass that configuration in when configuring fluent NHibernate.

string configFile = "hibernate.cfg.xml";

//setup the normal map configuration
Configuration normalConfig = new Configuration().Configure(configFile);

//setup the fluent map configuration
Fluently.Configure(normalConfig)
.Mappings(
m => m.FluentMappings
.ConventionDiscovery.Add(DefaultLazy.AlwaysFalse())
.AddFromAssemblyOf<UserMap>())
.BuildConfiguration();
Testing with a Compiled Class that Doesn’t Implement an Interface – Adapter Pattern