Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
add comment (requires an account with 50 reputation)

4 Answers

up vote 1 down vote accepted

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

What version of EF are you using ?

share|improve this answer
add comment (requires an account with 50 reputation)

If an EF object has been created outside a context, you need to Attach the object to the context.

See: http://msdn.microsoft.com/en-us/library/bb896271.aspx

share|improve this answer
If I wait to attach after I'm done with my object, don't I lose the benefits of EF's concurrency check? Also, does that Attach look at my object and sync it up to one in the db? – RailRhoad Dec 29 '09 at 16:29
Plus, attaching an object to the context leaves it an unchanged state. Wouldn't I want to query it first (before the model binding) and let it bind then. Then it could properly determine the state right? – RailRhoad Dec 29 '09 at 16:33
The Attached object will be a new object, if you want to update an existing object, you need first to get it from the database, make the changes and then save the changes. – Shiraz Bhaiji Dec 29 '09 at 16:57
add comment (requires an account with 50 reputation)

I know this is an old thread but in the interest of new readers:

Based on my observations using VS 2012, MVC 4 and EF 4.0 with a view that has an EF object for a model that submits a form back to the controller.

On the controller:

public ActionResult SubmitEFObject(tblData data, FormCollection col)

"data" will only have the properties used in the view (@Html.xxxFor) filled.

It appears that when "data" is created, the posted FormCollection is used to set data's properties. If you had a property that wasn't used, DataID for example, then data.DataID will have a null/default value. Add a "@Html.Hidden(m => m.DataID)" to your view and THEN DataID will be filled.

As a 'quick n dirty' way to work with this, I created a method that would merge the incoming 'data' with the 'data' in the database and return the merged object:

// Note: error handling removed
public tblData MergeWithDB(DBContext db, tblData data, params string[] fields)
{
  tblData d = db.tblData.Where(aa => aa.DataID == data.DataID).Single();
  if (fields.Contains("Field1")) d.Field1 = data.Field1;
  if (fields.Contains("Field2")) d.Field2 = data.Field2;
  if (fields.Contains("Field3")) d.Field3 = data.Field3;
  // etc...
  return d;
}

On the controller:

public ActionResult SubmitEFObject(tblData data, FormCollection col)
{
  DataEntities db = new DataEntities();
  tblData d = MergeWithDB(db, data, col.AllKeys);
  db.SaveChanges();
}

You could make this more generic using reflection or maybe more efficient by looping through the string[] fields instead of all the ifs but for my purposes this was 'good enough'.

share|improve this answer
add comment (requires an account with 50 reputation)

Database work should be put in the repository call.

Are you directly binding to an entity framework object in your model binding? If not, you should consider do some mapping between your custom object and entity framework object.

share|improve this answer
My db calls are in repository. What I'm trying to find out is when to call them to best leverage EF. As far as your question goes, I'm trying to determine that with these questions. It looks like I best use EF if I model bind querying the EF object early so it can use concurrency and better track state. – RailRhoad Dec 29 '09 at 16:36
I wouldn't expose the EF object directly to "model binding", I will use my own domain object or DTO, and map those object onto the EF object. – J.W. Dec 29 '09 at 18:38
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.