Reputation: 16908
Can we update Id fields in NHibernate like the following?
MyClass myObj = MyClass.Retrieve(1);
myObj.Id = 999;
myObj.Name = "name name";
myObj.Value = 1000;
MyClass.Update(myObj);
Upvotes: 1
Views: 1076
Reputation: 1
I had to add a Evict
after delete then work.
Something like this will work:
ISession.Delete(yourClass);
yourClass.Id = somethingelse;
ISession.Save(yourClass);
Why would you want to change an Identifier? It sounds like a very bad thing to do.
This is what I did:
ISession.Delete(yourClass);
ISession.Evict(yourClass);
yourClass.Id = somethingelse;
ISession.Save(yourClass);
Upvotes: 0
Reputation: 171178
You can close the session, execute raw sql to change the primary key, and then open a new session. that is the only thing that works as nhibername relies on primary key identity. you will never ever be able to make nhibernate do it.
Upvotes: 2
Reputation: 49261
You should never change the identifier (primary key) on an existing object. With NHibernate, you should only assign the identifier on new objects if you've mapped the generator as "assigned". If you're intent is to clone an existing object, then retrieve the object and create the clone as a new instance so that a new identifier is assigned.
Upvotes: 3
Reputation: 8381
Something like this will work:
ISession.Delete(yourClass);
yourClass.Id = somethingelse;
ISession.Save(yourClass);
Why do you want to change an Identifier? It sounds like a very bad thing to do.
Upvotes: 2