开发者

why Entity framework Entity Master Details Entity Edit

开发者 https://www.devze.com 2023-03-15 05:58 出处:网络
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 posthttp:

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 hav开发者_开发知识库e 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


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消