开发者

Detecting changes on entities in contex of asp.mvc

I have very standart scenario. I send entity from controller to view for user to edit it. User pushes submit button I get entity back from browser in my controller. Its detached now so I attach it back to context. How can I detect if there we开发者_开发百科re changes made compared to database?


You need to pull the entity back from the database when the user submits. There are a number of reasons you can't know for certain what to do just by looking at what came back:

  1. Can't trust data from the user. The user has full control over what the browser sends back to you, so this is a possible exploit if you key off something in the request
  2. Another user might have modified the same data

Pulling an entity from the database is fast, so just pull it, set the fields you want to set and SaveChanges()

Addition: As Eduard noted in the comments below, it looks like there is a utility method ApplyCurrentValues to do this: msdn.microsoft.com/en-us/library/dd487246.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜