nhibernate - 使用代码映射生成 HBM 文件

标签 nhibernate nhibernate-mapping

我正在通过代码使用 NHibernate 映射,并以这种方式创建 session 工厂:

            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 文件。

我怎样才能做到这一点?

最佳答案

两种方式:-

//This will write all the XML into the bin/mappings folder  
mapper.CompileMappingForEachExplicitlyAddedEntity().WriteAllXmlMapping();  

请注意上述方法,因为当在 bin 文件夹中检测到更改时,您的 asp.net 应用程序将回收,另一种方法是:-

var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();  
//you could add a breakpoint here!   
var mappingXml = mapping.AsString();  

关于nhibernate - 使用代码映射生成 HBM 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8669128/

相关文章:

nhibernate - Fluent NHibernate 的仅查询属性

nhibernate - 由于计算属性的小数精度导致幻影更新

c# - NHibernate MySQL 映射设置列类型

fluent-nhibernate - NHibernate 异常 : could not initialize a collection, 无效的列名。流畅的映射。也许是多对一的问题?

nhibernate - Nhibernate 映射文件中的硬编码值

c# - NHibernate 映射域 "extension properties"?

Linq for NHibernate 和预加载的 fetch 模式

.net - LINQ-to-NHibernate 准备好用于生产代码了吗?

asp.net - 如何解决 NHibernate 中批量更新返回意外行计数错误?

fluent-nhibernate - 使用 Fluent NHibernate 映射一个简单的元素集合