2

Fluent NHibernate 中的 HBM 导出功能似乎不起作用。

如果我调用 FluentMappingsContainer.ExportTo,生成的映射不正确,我得到以下异常:

FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

我的配置代码如下所示:

        MsSqlConfiguration database = MsSqlConfiguration.MsSql2008
            .ConnectionString(GetConnectionString())
            .Cache(c => c
                            .UseQueryCache()
                            .UseSecondLevelCache()
                            .ProviderClass<SysCacheProvider>()
            );

        database.ShowSql();

        FluentConfiguration config = Fluently.Configure()
            .Database(database)
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Entity>()
                               .Conventions.AddFromAssemblyOf<Entity>());

        config.ExposeConfiguration(x =>
        {
            x.SetProperty("hbm2ddl.keywords", "auto-quote");
            x.SetInterceptor(new ServiceInterceptor());
        });

        config.ExposeConfiguration(x => { x.SetProperty("current_session_context_class", "thread_static"); });

        // Configure HBM export path, if configured:

        var path = Service.Config.HbmExportPath;

        if (!String.IsNullOrEmpty(path))
            config.Mappings(m => m.FluentMappings.ExportTo(path));

        // Build session factory:

        _sessionFactory = config.BuildSessionFactory();

将我的配置中的 HbmExportPath 设置为 null,应用程序启动并运行没有问题。一旦我配置了导出路径(导致调用 ExportTo),生成的映射就会导致如上所述的异常。

查看导出的映射,似乎没有应用我的约定 - 例如,我有一个外键约定,使用驼峰式和“Id”后缀,但是当我导出 HBM 文件时,主键是一致的用下划线和小写“_id”命名,例如:

<class xmlns="urn:nhibernate-mapping-2.2" name="MyApp.Entities.Contact, MyApp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Contact`">
  ...
  <bag name="Departments" table="ContactDepartment">
    <key>
      <column name="Contact_id" />
    </key>
    <many-to-many class="MyApp.Entities.Department, MyApp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
      <column name="Department_id" />
    </many-to-many>
  </bag>
  ...
</class>

我在以前的版本和 Fluent 的当前版本中遇到了这个问题。

有任何想法吗?

4

1 回答 1

3

在浏览 Fluent 源代码(来自 Git 存储库的最新版本)之后,我觉得有些奇怪。

ExportTo() 方法被定义了两次——一次是由 FluentConfiguration 本身定义的,它确实“过早”地导出了配置文件,导致配置不完整,无论是在运行时(导致上述异常)还是在导出时——时间。

奇怪的是,PersistenceModel 类型确实具有导出完整配置的能力,但没有公开此功能。相反,ExportTo() 至少还有另外两个看似损坏的实现。

为了解决这个问题,我们需要访问 PersistenceModel 实例,该实例具有编写完整配置的能力——幸运的是,我找到了一种方法:

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

现在可以正确输出 HBM 文件!

于 2011-12-12T22:42:32.887 回答