Getting NHibernate to save or update using column other than the <id>
How do I get NHibernate to save or update using column other than the <id>
? I've implemented an identity key column in a table but this is not what makes a row unique. I know that "composite-id" exists but I heard that that should only be used on legacy databases, where there is not much freedom to change it. Is there any other way to implement unique keys without using an artifical key? Should I make my own key based on a hash of the columns that make a row unique? 开发者_Python百科 If I do that, won't NHibernate complain when it tries to insert an object with a manually assigned key?
NHibernate has lots of options for primary keys. You can use any of the available built-in generators, or an application-assigned PK (the "assigned" generator option).
If none of these options suit you, you can always implement your own IIdentifierGenerator
, but think really hard before doing so: the built-in generators and PK handling encapsulate years of RDBMS experience from many experts.
Composite PKs are generally not worth the complexity they add.
Uniqueness of db rows is completely different then identifying which row you want to update.
Why not use unique constraints to ensure your records are unique and follow the standard practice of using Ids.
精彩评论