2011-12-12 39 views
2

Fluent NHibernate中的HBM导出函数似乎不起作用。流利的NHibernate(1.2.0.712)导出映射到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我配置为空,应用程序启动和运行没有问题。只要我配置了导出路径(导致调用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> 

我有这个问题与以前的版本,并用流利的当前版本。

任何想法?

回答

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文件目前正在输出正确!

+0

作者(詹姆斯格雷戈里)证实,这个功能很粗糙,需要一些工作。所以这种解决方法是现在的方式 - 我正在关闭这个线程,并希望这个答案对其他人有用。 –

+0

谢谢,我刚开始看了一些导出的hbm.xml文件,我以为我疯了,想知道为什么我的表命名约定丢失了。这解决了我的问题! – dotjoe