I have an application using NHibernate and I’m using Fluent NHibernate to mapping my entities. It works fine, but, I want to create the SessionFactory using the native way of NHibernate because my team will ues this library on other projects, so we need this flexibilty to move the nhibernate.cfg.xml. My question is: How can I set the Fluent Mappings in the configuration of SessionFactory with native way of nhibernate?
I try something like this on my configuration method:
private static ISessionFactory Configure()
{
if (_factory != null)
return _factory;
var configuration = new Configuration().Configure();
// I could set my assembly of mapping here, but it's on our internal framework
var fluentConfiguration = Fluently.Configure(configuration)
//.Mappings(c => c.FluentMappings.AddFromAssembly(typeof(ProductMap)))
.BuildConfiguration();
_factory = fluentConfiguration.BuildSessionFactory();
return _factory;
}
I tried to set it by xml, but it does not work.
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<!-- other configs here...-->
<mapping assembly="MyApplication.Data.Mapping" />
</session-factory>
</hibernate-configuration>
I don’t know if is there any way to get this mapping setted on the xml and pass to FluentConfiguration declaration on my method to create the ISessionFactory.
Thank you guys.
The mapping in the config does not work because it will not consider Fluentmappings (Nhibernate does not know FluentNhibernate). You have to set it by code. Best option i can think of is to implement a hook to alter the config object before building the sessionfactory: