简体   繁体   中英

Generate identity from seed table in Fluent NHibernate

I need to generate an identity from a column in a seed table and increment the value in that column.

eg

Seed Table:

name     "analysisid"
id       1


Analysis Table:

id
name
description

On the analysis mapping I need to ensure that the id is taken from the seed table and on inserting an analysis the analysisid of the seedid is updated.Do I implement a custom class inheriting from TableGenerator?

NHibernate includes a number of identity generation strategies , and includes an extension mechanism for custom ones via NHibernate.Id.IIdentifierGenerator .

You could do this by implementing an insert trigger which fetches and assigns the next identifier for entity Analysis. In this case use <generator class="trigger-identity" /> .

Another approach is to build your own identifier strategy by implementing IIdentifierGenerator and mapping as <generator class="My.Namespace.MyIdentifierGenerator" /> .

In fluent, those generators are mapped by:

Id( x => x.Id ).GeneratedBy.Custom<NHibernate.Id.TriggerIdentityGenerator>();
Id( x => x.Id ).GeneratedBy.Custom<My.Namespace.MyIdentifierGenerator>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM