使用实体框架进行模型绑定(ASP.NET MVC) [英] Model Binding With Entity Framework (ASP.NET MVC)

查看:82
本文介绍了使用实体框架进行模型绑定(ASP.NET MVC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前,我创建了一个AddClient页面,该页面(在发布时)传递了一个客户端对象,并且我在存储库中使用了db.AddToClient(obj)对其进行持久化.简单的东西.

Earlier I created an AddClient page that (when posted) passed a client object and I used db.AddToClient(obj) in my repository to persist it. Easy stuff.

现在,我有一个详细信息页面,该页面的保存将帖子提交给操作"UpdateClient".在执行该操作之前,我的自定义模型联编程序会创建我的Client对象,并将其方便地移交给该操作.问题是,此客户端对象尚未连接到EF上下文.正确的位置在哪里?在modelbinder中,或者也许当我们从控制器中获取它时,或者也许等到我们进行存储库调用并将其链接到那里?推荐的过程是什么?

Now, I have a details page whose save submits a post to an action "UpdateClient". Before that action is hit, my custom model binder creates my Client object and it's conveniently handed to the action. The thing is, this client object is not connected to a EF context yet. Where is the correct place to do that? In the modelbinder, or maybe when we get it from the controller, or perhaps we wait until we do a repository call and link it up there? What's the recommended process?

推荐答案

根据我的记忆,您将需要再次将该对象附加到上下文并将其设置为已修改

From what I remember, you will either need to attach the object again to the context and set it as modified

或从数据库中重新加载对象并应用您的更改.

Or reload the object from the database and apply your changes.

本文对此进行了更好的解释:

This article explains it better:

http://msdn.microsoft.com/en-us/magazine/ee321569.aspx#id0090022

您使用的是什么版本的EF?

What version of EF are you using ?

这篇关于使用实体框架进行模型绑定(ASP.NET MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆