流畅的NHibernate(1.2.0.712)导出映射到HBM不工作/不遵守约定 [英] Fluent NHibernate (1.2.0.712) export mappings to HBM not working / not respecting conventions

查看:215
本文介绍了流畅的NHibernate(1.2.0.712)导出映射到HBM不工作/不遵守约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



如果我调用FluentMappingsContainer.ExportTo,生成的映射出来不正确,我得到以下例外:

  FluentNHibernate.Cfg.FluentConfigurationException:创建SessionFactory时使用无效或不完整的配置。检查PotentialReasons集合和InnerException以获取更多详细信息。 

我的配置代码如下所示:

  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<实体>());

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

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

//配置HBM导出路径,如果配置:

var path = Service.Config.HbmExportPath;

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

//构建会话工厂:

_sessionFactory = config.BuildSessionFactory();

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



查看导出的映射,看起来我约定不被应用 - 例如,我有一个外键约定,使用骆驼壳和Id后缀,但是当我导出HBM文件时,主键一贯用下划线命名,小写_id ,例如:

 < class xmlns =urn:nhibernate-mapping-2.2name =MyApp.Entities.Contact ,MyApp,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = nulltable =`Contact'> 
...
< bag name =Departmentstable =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版本有这个问题。



任何想法?

解决方案

浏览Fluent源代码(最新的Git仓库)对于我来说,有些东西看起来很奇怪。



ExportTo()方法定义了两次 - 一次由FluentConfiguration本身定义,并且它的出现似乎正在导出配置文件太早 ,导致运行时(导致上述异常)和导出时的配置不完整。



奇怪的是,PersistenceModel类型确实具有导出完整配置,但此功能不会公开。相反,至少有两个其他似乎破坏的ExportTo()实现。



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

  //创建一个PersistenceModel类型的本地实例:
PersistenceModel model = new PersistenceModel();

FluentConfiguration config = Fluently.Configure()
.Database(数据库)
.Mappings(m => m.UsePersistenceModel(model)//使用本地实例!
.FluentMappings.AddFromAssemblyOf< Entity>()
.Conventions.AddFromAssemblyOf< Entity>());

// ...

var path = Service.Config.HbmExportPath;

_sessionFactory = config.BuildSessionFactory(); //完成配置

//现在从PersistenceModel中写出完整的映射:

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

HBM文件正在正确输出!


The HBM export function in Fluent NHibernate does not seem to work.

If I call FluentMappingsContainer.ExportTo, the generated mappings come out incorrect, and I get the following exception:

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

My configuration code looks like this:

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

Setting HbmExportPath in my configuration to null, app launches and runs without problems. As soon as I configure the export path (causing ExportTo to be called), the generated mappings cause an exception as described above.

Looking at the exported mappings, it appears my conventions are not being applied - for example, I have a foreign-key convention in place, using camel-case and "Id" suffix, but when I export the HBM files, primary keys are consistently named with an underscore and lowercase "_id", for example:

<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>

I had this problem with the previous version and with the current release of Fluent.

Any ideas?

解决方案

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!

这篇关于流畅的NHibernate(1.2.0.712)导出映射到HBM不工作/不遵守约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆