Using Fluent NHibernate To Export / Create .hbm Files (NHibernate Mapping Files)
private static ISessionFactory CreateSessionFactory()
{
string SchemaExportPath = Path.Combine(System.Environment.CurrentDirectory, "Mappings");
if (!Directory.Exists(SchemaExportPath))
Directory.CreateDirectory(SchemaExportPath);
return Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
.Mappings(M => M.FluentMappings.AddFromAssemblyOf<SessionFactory>()
.ExportTo(SchemaExportPath))
.ExposeConfiguration(Cfg => _configuration = Cfg)
.BuildSessionFactory();
}
There may be times when you would like to inspect the NHibernate mapping files generated by Fluent NHibernate to debug mapping issues. Luckily enough it’s quite easy and you will just need to add a few lines of code when creating the ISessionFactory.
Firstly you will need to ascertain the absolute location of the bin directory by calling System.Environment.CurrentDirectory and then combining the path with a folder named Mappings. (Note: You can put the mappings in any location you like I just prefer a subfolder in the bin directory!)
You will then need to run a quick check to see if the directory exists and if it doesn't then create it.
When configuring the ISessionFactory and setting the assembly to obtain the fluent mappings from just add a call to the method ExportTo(PathYouWantToExportTo) and then the mappings will be created in the location specified.