我正在通过代码使用NHibernate映射,我正在以这种方式创建会话工厂:
var mapper = new ModelMapper();
mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());
HbmMapping domainMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
const bool executeScript = false;
var configuration = new Configuration();
configuration.DataBaseIntegration(c =>
{
c.Dialect<MsSql2005Dialect>();
c.ConnectionString =
ConfigurationManager.ConnectionStrings["ShopConnectionString"]
.ConnectionString;
c.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
});
configuration.AddMapping(domainMapping);
_sessionFactory = configuration.BuildSessionFactory();
我需要获取相应的HBM文件。
我怎样才能做到这一点?
答案 0 :(得分:4)
两种方式: -
//This will write all the XML into the bin/mappings folder
mapper.CompileMappingForEachExplicitlyAddedEntity().WriteAllXmlMapping();
请注意上面的这种方法,因为您的asp.net应用程序将在您的bin文件夹中检测到更改时进行回收,另一种方式是: -
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
//you could add a breakpoint here!
var mappingXml = mapping.AsString();
答案 1 :(得分:1)
使用AsString()扩展方法:
domainMapping.AsString()
它将为您提供可以保存到文件中的xml。您可以调用该方法,例如在构建SessionFactory之前。