R – Model Binding With Entity Framework (ASP.NET MVC)


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.

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?

Best Solution

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:


What version of EF are you using ?