0

I work with on MVC project using entity framework with repository and Unit of work pattern, I'm trying to edit variable length collection in edit action(master details) like example in this post http://ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1

My problem is every time submit form to edit action the child collection DB table duplicate child collection rows and set foreign key of old ones to null,and modify the master entity successfully . i check the entity state of the master entity is deatached(child collection also deatached).

I have work around to loop throw child collection and change it's state to modified or added or deleted,But what i need to know what is the best solution this problem to automatically detect changes or not to change entity state to deatached.

Thanks

A Seif
  • 1

1 Answers1

0

EF context doesn't know what changes your client did and EF has no mechanism to find these changes. There are only two approaches to handle this scenario:

  • Manually set state of each entity in object graph as you do it know (it is not workaround it is official solution).
  • Load the entity graph from the database first and use TryUpdateModel to let MVC merge new state and old state for you as described here.
Community
  • 1
  • 1
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
  • I try using TryUpdateModel work fine,but still one problem it duplicates the child rows on DB tables ,every time update action executed it duplicate row on child collection Db table,put all old rows foreign key to null and the new rows with correct parent foreign key value. – A Seif Jun 26 '11 at 11:15
  • Are you loading the entity with its childs? – Ladislav Mrnka Jun 26 '11 at 11:16
  • I think it delete all child entities(set foreign key value to NULL) and then insert it again. – A Seif Jun 26 '11 at 12:02