Tuesday, December 28, 2010

Export mapping files generated by Fluent NHibernate

If you are using  NHibernate, you probably also heard about Fluent NHibernate which allows you to create your object-relational mapping from a mix of conventions and code instead of using error-prone XML files. Although you’re using a completely different API under the hood the same old XML files still exist. Sometimes it would be handy to view the NHibernate mapping files generated by Fluent NHibernate.It turns out that this is quite easy and you will just need to add a call to the method ExportTo() and then the mappings will be created in the location specified.

   1:  private static ISessionFactory CreateSessionFactory() 
   2:  { 
   3:     string outputdir= Path.Combine(System.Environment.CurrentDirectory, "Mappings"); 
   5:      return Fluently.Configure() 
   6:        .Database(SQLiteConfiguration.Standard.InMemory().ShowSql()) 
   7:        .Mappings(M => M.FluentMappings.AddFromAssemblyOf<SessionFactory>() 
   8:        .ExportTo(outputdir))
   9:        .BuildSessionFactory(); 
  10:  }

Remark: Make sure that the directory exists where you want to output the generated mapping files.

No comments: