MVC3: Repository Updates and ObjectStateManager?

The primary difference between your two methods is that the Admin Edit method creates a new Article from your AdminEditViewModel, then it attaches this newly created Article to your database. This works because it's a new object that has never been attached to a dc In the second case, you get an Article from the repository, update that Article, then try and attach it again, this fails because it's not a newly created Article, it's an Article returned from the db Context in the first place so it's already attached. And you are trying to attach it again.

The primary difference between your two methods is that the Admin Edit method creates a new Article from your AdminEditViewModel, then it attaches this newly created Article to your database. This works because it's a new object that has never been attached to a dc. In the second case, you get an Article from the repository, update that Article, then try and attach it again, this fails because it's not a newly created Article, it's an Article returned from the db Context in the first place so it's already attached.

And you are trying to attach it again.

Should I refactor the Edit method to something like: Article article = articleRepository. GetByID(id); and map the ViewModel to that article object instead? It seems a bit odd to query for the object when all of the information I need is in the ViewModel already.

– johnh Jul 23 at 22:17 Re: my comment above, I just tried that and it makes no difference. However, it did illustrate what you said above about the way the context is constructed between requests, but now I really don't know how to solve that. Thanks a lot for the help so far.

– johnh Jul 23 at 22:50 1 @johnh - There are several solutions that are hackish. For instance, you could detach the article after you get it from the repository. However, I think you want to design a better solution overall and that requires knowing a lot more about the design than is feasible to post here.

– Mystere Man Jul 24 at 0:08 I've marked this as the answer as you're correct, the design does need to be improved. I've spent the past 7 hours working on that and that's become quite apparent. Thank you very much for the help.

You've put me on the right track. :) – johnh Jul 24 at 7:29.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions