Fluent NHibernate (1.2.0.712) export mappings to HBM not working / not respecting conventions

孤街醉人 提交于 2019-12-01 11:06:10

After drilling through the Fluent source code (latest from Git repository), something looks odd to me.

The ExportTo() methods are defined twice - once by FluentConfiguration itself, and it does appear it's exporting the configuration files "too soon", resulting in an incomplete configuration, both at run-time (resulting in the above exception) and at export-time.

Oddly, the PersistenceModel type does have the capability to export the complete configuration, but this feature is not exposed. Instead, there are at least two other seemingly broken implementations of ExportTo().

To solve the problem, we need access to the PersistenceModel instance, which has the capability to write the complete configuration - fortunately, I found a way to do that:

        // create a local instance of the PersistenceModel type:
        PersistenceModel model = new PersistenceModel();

        FluentConfiguration config = Fluently.Configure()
            .Database(database)
            .Mappings(m => m.UsePersistenceModel(model) // use the local instance!
                               .FluentMappings.AddFromAssemblyOf<Entity>()
                               .Conventions.AddFromAssemblyOf<Entity>());

        // ...

        var path = Service.Config.HbmExportPath;

        _sessionFactory = config.BuildSessionFactory(); // completes the configuration

        // now write out the full mappings from the PersistenceModel:

        if (!String.IsNullOrEmpty(path))
            model.WriteMappingsTo(path);

The HBM files are now being output correctly!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!