0

之前有人问过这个问题,但答案都显示了如何从 fluentnhibernate 导出 hbm 文件。我们正在使用包装流利的 S#arpArchitecture。我能够导出架构,但我真正想要的是 xml 文件来解决错误。我之前使用 FNH 完成了此操作,但是将 S#arp 添加到混合中会很复杂,我无法弄清楚。

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

4

3 回答 3

4

Here is how I do it in one of my projects:

[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);
    }

You will need to set the two keys in the your app config or provide values directly for them.

于 2010-07-25T00:41:18.960 回答
0

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

在 Mappings 调用中,您可以执行以下操作:

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

  m.AutoMappings
    .Add(/* ... */)
    .ExportTo(@"C:\your\export\path");
})
于 2011-02-14T09:37:44.923 回答
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); 
} 

您必须确保您的配置设置正确,并且您为目录选择了合适的位置,否则这应该可以工作。它对我有用。

于 2012-06-08T16:24:39.927 回答