代码之家  ›  专栏  ›  技术社区  ›  Maggie

如何使用带有fluent映射的arps架构导出hbm xml文件

  •  0
  • Maggie  · 技术社区  · 14 年前

    This question 以前被问过,但答案都显示了如何从fluentnhibernate导出hbm文件。我们使用的是S#ARP架构,它包裹了fluent。我能够导出模式,但我真正想要的是xml文件来解决错误。我以前用过FNH,但是在混合中加入S#arp会有复杂的事情,我无法理解。

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

    3 回复  |  直到 8 年前
        1
  •  4
  •   Alec    14 年前

    以下是我在一个项目中的做法:

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

        2
  •  0
  •   Igor Zelmanovich    14 年前

    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");
    })
    
        3
  •  0
  •   Michael    13 年前

    事实证明,只有在不使用自动映射的情况下,它才有效。以下是使用自动映射的解决方案:

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

    您必须确保您的配置设置正确,并且为目录选择了适当的位置,否则应该可以正常工作。对我来说是这样。