Replies: 1 comment
-
Try with |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi everyone, I'm new to NHibernate so maybe this is something easy but I just can't find the way to do it properly.
Is there a way to save the loaded entity as a new insert back to DB?
For example, If I have a class model with one-to-many submodels:
In the first session, I load one of the Models with Model model = session.Load< Model>(xxx), and continue using the model after the session is closed.
At some point, I need to save the model back to the database but as a NEW insert (not update rows in DB from where I loaded in 1st session) in the new session. What is the right way to do this?
I guessed that I should just change model.Id and model.submodels.ids back to 0 manually and use Save(). When i do this, model is inserted as new, but model.Id is never updated and stays on 0 which causes errors when inserting submodels as reference to model.id cant be null.
The solution I have found to work is to just make a new Model instance and copy all properties from the original model except Id and save that one instead, and change only model.submodels.ids to 0.
Weird thing is that submodels loaded with model are inserted normally after I just change ID to 0 and ID gets updated as well (don't need to create new class instances for each of them), so the problem seems to occur only to the "parent" entity that got loaded.
Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions