I have an MVC2 app where I am starting to use the STE's. I am looking for some clarification on how updates should work.
Background:
If I have a Blog entity with related category entities and Related post/comment entities. In MVC I am rendering a view with the main Blog entity and the categories but not the related posts. When I post back the Blog entity to the server I can see the related categories but not the posts (since they were not in the view) in the entity being serialized back to the server. Also, the Blog entity has a change state of 'Added'.
I then try to call Applychanges()
and SaveChanges()
on this Blog entity to the perform an update and it fails because of the FK relationship with posts and the fact that there are related posts in the database but not attached to the entity I am sending back.
With some further testing...If I grab a current instance of the Blog entity (with all the related FK entities) while connected to the server (state=unchanged), modify a property (state=Modified) and update it works as expected.
So my questions: If I have related entities that are not being rendered in a view and therefore not post back with the Blog entity should the update work?
Why does the Blog entity get post back with an 'Added' status and not 'Modified'? I would assume it would come back with a 'Modified' changedstate for all changed entities and then when I called ApplyChanges/SaveChanges()
only the modified items would attempt to update and this is why I would not need all related entities.
Should I be able to pass an entity directly from the client and ApplyChanges()/SaveChanges()
or should I be posting back to the server the entity, grabbing an existing copy from the database, applying changes to that copy and then posting the existing object back?