Entity Framework 4 merge changes between two Entities
I use the ObjectDataSource in an ASP.NET Application.
Using the ASPxGridView. When Updating it goes back to the Data Access Layer and tries to update the Entity, now as I can see while having some properites (Columns, Visible = false) when the entity arrives in the update method the visible = false columns have no values.
I don't want to show all the columns...what if I need 3 of the 30 columns? So I thought I would get the original entity from the context and merge the 开发者_开发百科differences from the updated entity.
Any idea if this could happen using the Entity Framework ? Or Any() :)
Thank you
You are using detached entities and you modify only selected properties => you are the only one who knows which properties were modified and you must write a code to copy values from these properties to updated entity. There are different ways for updates of selected properties - you can use simple approach or you can build repository.
Edit:
If you want to get whole object when calling update on ObjectDataSource
you must pass whole object to grid (to client). It means you have bind all object properties to some controls. The easy approach is to use HiddenField
controls. But in case of hidden fields you can't be sure that user will not modify posted values which would result in unwanted update. For example suppose that you post product price into hidden field. If user uses some tool to intercept request and change the price, you will update it into your database!
Another approach is to create your custom web control which will store value in ViewState
instead of plain hidden input.
精彩评论