NHibernate - how to switch dynamically the generator for Id?

和自甴很熟 提交于 2019-12-25 10:06:27

问题


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

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

Is this possible? How?


回答1:


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

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.




回答2:


It's not clear why you would need to keep existing id's. I think you should not be needing to keep existing id's. Maybe you need to keep alternate id's instead?

If the previous system has it's own database, then you: 1) Need another mapping for the other table in the other database 2) Copy the data to your existing database (with key identity)

Which means you will need new id's anyway. Example: Suppose you want to copy a table of 'airlines' and the previous system uses the 'airline-code' as the primary key. You could use an integer as primary key in your new database and the airlinecode as your alternate key.



来源:https://stackoverflow.com/questions/5089066/nhibernate-how-to-switch-dynamically-the-generator-for-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!