0

This question was asked before but the answers all show how to export the hbm files from fluentnhibernate. We are using S#arpArchitecture which wraps fluent. I am able to export the schema but what I really want is the xml files to troubleshoot errors. I've done this using FNH before but adding S#arp to the mix has complicated things where I cannot figure it out.

I've found this question asked on several forums, but I can't find one that shows how to get the mapping files.

Community
  • 1
  • 1
Maggie
  • 1,546
  • 16
  • 27

3 Answers3

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.

Alec
  • 691
  • 4
  • 12
0

As it turns out that only works if you're not using automapping. Here's the solution if you're using automapping:

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

You'll have to make sure that your configuration is set up correctly and that you choose an appropriate location for the directory, but otherwise this should work. It did for me.

Michael
  • 168
  • 2
  • 9
0

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

In the Mappings call, you can do the following:

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

  m.AutoMappings
    .Add(/* ... */)
    .ExportTo(@"C:\your\export\path");
})
Igor Zelmanovich
  • 829
  • 10
  • 6
  • Thanks for your answer, but I could not use this method, which I linked to in my original question, which is why I asked the question. Alec answered the question for how to do the export within the Sharp architecture framework. – Maggie Feb 17 '11 at 16:53