Is it possible to map a composite key in fluent nhibernate where half of the key is an identity field in the database?

醉酒当歌 提交于 2019-12-11 23:12:00

问题


As the question states, is it possible to map a composite key in fluent nhibernate (or non fluent i suppose) where one of the two fields used in the composite is an identity field?

I have a table where one part of the primary key is an identity field and the other is a tenant id. This is a legacy database where the composite key is used as foreign keys all over, so modifying the database would be quite significant.

thanks!


回答1:


If it's really an identity column, then it's guaranteed to be unique for every record, right?

If so, you can ignore the tenancy ID completely for mapping purposes. Your identity fields will be unique in your primary table - AND your foreign key references will contain this unique Id, so the tenancy ID is actually irrelevant.

NHibernate doesn't really care whether you're mapping your database schema exactly. I've worked with legacy databases before where I've defined a unique constraint on a non-key column and then told NHibernate to use that as the primary key - it works, and it can neatly sidestep all kinds of horrific composite-key relationship mappings.

Think about it. Counter-intuitive, but it will probably work.



来源:https://stackoverflow.com/questions/1615647/is-it-possible-to-map-a-composite-key-in-fluent-nhibernate-where-half-of-the-key

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