I'开发者_如何学Cm using SelfTracking in entity framework.
The problem is that when I save data everything works fine but when I save it second time it want to add new object but I only want to update old one or don't do anything if the one wasn't modified.
I will describe it below:
I have User entity. I create it, do sth and then I create Context, Save it and the destroy Context but User isn't destroyed.
It works ok, User is saved in Database. Then I invoke the same method. Create new context, save and there is duplicate key violation (it doesn't know that it has to update user but tries to insert it).
Context.User.ApllyChanges(MyUser);
Context.SaveChanges();
I tried different variations and each time there is the same error. What am I doing wrong ?
Thanks for help.
When I click save new Context is created but I have the same EntityObject which I had. Then it wants to add new object
I've worked with POCO (not self tracking) but I believe it also applies.
The thing is that you must know if it is a New or and Update and add the object to the context and change its state to modified. If you leave it as added EF will try to insert it into the db.
精彩评论