Fluent NHibernate(1.2.0.712)将映射导出到HBM不工作/不遵守约定
Fluent NHibernate中的HBM导出功能似乎不起作用 如果调用FluentMappingsContainer.ExportTo,生成的映射结果不正确,我会得到以下异常:Fluent NHibernate(1.2.0.712)将映射导出到HBM不工作/不遵守约定,nhibernate,fluent-nhibernate,export,fluent,hbm,Nhibernate,Fluent Nhibernate,Export,Fluent,Hbm,Fluent NHibernate中的HBM导出功能似乎不起作用 如果调用FluentMappingsContainer.ExportTo,生成的映射结果不正确,我会得到以下异常: FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons coll
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();
MsSqlConfiguration数据库=MsSqlConfiguration.MsSql2008
.ConnectionString(GetConnectionString())
.Cache(c=>c
.UseQueryCache()
.UseSecondLevelCache()文件
.ProviderClass()
);
ShowSql();
FluentConfiguration配置=Fluently.Configure()
.数据库(数据库)
.Mappings(m=>m.FluentMappings.AddFromAssemblyOf())
.Conventions.AddFromAssemblyOf());
config.ExposeConfiguration(x=>
{
x、 SetProperty(“hbm2ddl.keywords”、“自动报价”);
x、 SetInterceptor(新的ServiceInterceptor());
});
ExposeConfiguration(x=>{x.SetProperty(“当前会话上下文类”,“线程静态”);});
//配置HBM导出路径(如果已配置):
var path=Service.Config.HbmExportPath;
如果(!String.IsNullOrEmpty(路径))
config.Mappings(m=>m.FluentMappings.ExportTo(path));
//生成会话工厂:
_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中遇到了这个问题
有什么想法吗?在浏览了流畅的源代码(Git repository的最新版本)之后,我觉得有些奇怪 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);
//创建PersistenceModel类型的本地实例:
PersistenceModel=新的PersistenceModel();
FluentConfiguration配置=Fluently.Configure()
.数据库(数据库)
.Mappings(m=>m.UsePersistenceModel(model)//使用本地实例!
.FluentMappings.AddFromAssemblyOf()的
.Conventions.AddFromAssemblyOf());
// ...
var path=Service.Config.HbmExportPath;
_sessionFactory=config.BuildSessionFactory();//完成配置
//现在写出PersistenceModel的完整映射:
如果(!String.IsNullOrEmpty(路径))
model.WriteMappingsTo(路径);
HBM文件现在正在正确输出 作者(James Gregory)确认此功能很粗糙,需要一些工作。因此,这是目前的解决方法-我正在结束这个线程,希望这个答案对其他人有用。谢谢,我刚开始查看一些导出的hbm.xml文件,我想我会发疯,想知道为什么我的表命名约定丢失了。这解决了我的问题!