Reputation: 2973
What I want to know is when NHibernate
assigns/generates a value to Id field. After an entity is saved into repository? I need to use a Product Id
to build a compound filename. But when I do use an Id
before saving entity into repository I get Guid.Empty
.
Thanks!
Upvotes: 2
Views: 2202
Reputation: 286
please see below link for your answer
http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-id
"But when I do use an Id before saving entity into repository I get Guid.Empty" -> id will only be generated once record it saved into database... if you want id before then i would suggest using assigned id generator, where you can generate your id before your record in saved into db.
Upvotes: 0
Reputation: 22424
I am wondering why you need to use the Id
before a save takes place? Surely using an Id
after it has been successfully committed to the database is the correct approach. After all if you use the Id
and then find the database has not persisted the entity then this will give you unwanted side effects.
However there is a work around but it is not very nice.
Set the id
generator to assigned. This comes with some caveats, you cannot use SaveOrUpdate
as NHibernate will not be able to detect that this is a new entity or one that needs updating. This means that you should either use the Save
or Update
methods explicitly
Upvotes: 4