NHibernate-如何动态切换ID的生成器? [英] NHibernate - how to switch dynamically the generator for Id?

查看:106
本文介绍了NHibernate-如何动态切换ID的生成器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我将所有映射都保存为hbm.xml.我想在运行时(应用程序启动)将某些实​​体的ID生成器的类型从标识"动态切换为分配".

Right now I have all my mappings as hbm.xml. I want to switch dynamically the type of Id generator for certain entities from 'identity' to 'assigned' at runtime (application start).

这是因为我需要支持从以前的系统导入数据并保留现有ID.

This is because I need to support importing data from previous system and keep existing ids.

这可能吗?怎么样?

推荐答案

生成器是映射的一部分,因此您需要在创建会话工厂之前更改映射.

The generator is part of the mappings, so you need to change the mappings before creating the session factory.

使用Fluent或ConfORM可以轻松做到这一点.可以在将XML映射提供给配置之前更改XML映射,但这很麻烦.

This is easy to do with Fluent or ConfORM. It's possible to change XML mappings before feeding them to the configuration, but it's cumbersome.

只需检查配置标志(在启动应用程序时您将对其进行更改),然后调用相应的生成器即可.

Just check for a configuration flag (that you'll change when starting the app), and call the appropriate generator.

这篇关于NHibernate-如何动态切换ID的生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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