Entity Framework 4 合并两个实体之间的更改
我在 ASP.NET 应用程序中使用 ObjectDataSource。
使用 ASPxGridView。更新时,它返回到数据访问层并尝试更新实体,现在我可以看到,当实体到达更新方法时,虽然有一些属性(列,Visible = false),visible = false 列没有值。
我不想显示所有列...如果我需要 30 列中的 3 列怎么办?所以我想我应该从上下文中获取原始实体并合并与更新实体的差异。
知道使用实体框架是否会发生这种情况吗?或任何():)
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用分离的实体并且仅修改选定的属性=>您是唯一知道哪些属性被修改的人,并且您必须编写代码将值从这些属性复制到更新的实体。更新所选属性有不同的方法 - 您可以使用 简单方法或者您可以构建存储库。
编辑:
如果您想在调用
ObjectDataSource
上的更新时获取整个对象,您必须将整个对象传递到网格(到客户端)。这意味着您已将所有对象属性绑定到某些控件。最简单的方法是使用HiddenField
控件。但如果存在隐藏字段,您无法确定用户不会修改发布的值,这会导致不必要的更新。例如,假设您将产品价格发布到隐藏字段中。如果用户使用某种工具拦截请求并更改价格,您会将其更新到您的数据库中!另一种方法是创建自定义 Web 控件,该控件将在 ViewState 中存储值而不是普通的隐藏输入。
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 useHiddenField
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.