2010-07-24 52 views
0

This question之前被问过,但答案都显示如何从fluentnhibernate导出hbm文件。我们正在使用包装流利的S#arpArchitecture。我能够导出架构,但我真正想要的是xml文件来排除错误。我之前使用FNH做过这个,但将S#arp添加到混音中有复杂的事情,我无法弄清楚。如何使用s#arparchitecture输出hbm xml文件与流利的映射

我在几个论坛上发现了这个问题,但我找不到一个显示如何获取映射文件的问题。

回答

4

这里是我如何做到这一点在我的项目之一:

[TestMethod] 
    public void CreateSchema() 
    { 
     var mappingOutput = ConfigurationManager.AppSettings["xmlMappingOutputDirectory"]; 
     var sqlOutput = ConfigurationManager.AppSettings["sqlOutputDirectory"]; 

     Configuration cfg = new Configuration().Configure(); 
     var persistenceModel = new PersistenceModel(); 
     persistenceModel.AddMappingsFromAssembly(Assembly.Load("ProjectName.Data")); 
     persistenceModel.Configure(cfg); 
     persistenceModel.WriteMappingsTo(mappingOutput); 
     new SchemaExport(cfg).SetOutputFile(sqlOutput).Create(true, false); 
    } 

你需要设置在你的应用程序配置的两个键,或直接为他们提供价值。

+0

谢谢,我需要什么名字空间 配置cfg = new Configuration()。Configure(); – Maggie 2010-07-25 01:54:31

+0

使用Configuration = NHibernate.Cfg.Configuration; – Alec 2010-07-25 03:29:46

+0

谢谢,我得到它的工作! – Maggie 2010-07-26 18:27:16

0

http://wiki.fluentnhibernate.org/Fluent_configuration#Exporting_mappings

在映射打电话,你可以做到以下几点:

.Mappings(m => 
{ 
    m.FluentMappings 
    .AddFromAssemblyOf<YourEntity>() 
    .ExportTo(@"C:\your\export\path"); 

    m.AutoMappings 
    .Add(/* ... */) 
    .ExportTo(@"C:\your\export\path"); 
}) 
+0

感谢您的回答,但我无法使用这种方法,这是我在原始问题中链接的,这就是我问这个问题的原因。亚历克回答了如何在夏普架构框架内进行导出的问题。 – Maggie 2011-02-17 16:53:33

0

事实证明,只有当你不使用自动映射的工作原理。下面是如果你使用自动映射解决方案:

public void CanGenerateMappingFiles() 
{ 
    DirectoryInfo directoryInfo = new DirectoryInfo("../../../../db/mappings"); 

    if (!directoryInfo.Exists) 
     directoryInfo.Create(); 

    Configuration cfg = new Configuration().Configure(); 
    var autoPersistenceodel = new AutoPersistenceModelGenerator().Generate(); 

    autoPersistenceodel.Configure(cfg); 
    autoPersistenceodel.AddMappingsFromAssembly(Assembly.Load("TrackerI9.Data")); 
    autoPersistenceodel.WriteMappingsTo(directoryInfo.FullName); 
} 

你必须确保你的配置是正确设置和所选择的目录中适当的位置,但除此之外,这应该工作。它为我做了。