0

I am using EF 4.3 and MVC 3.

In an Edit action, I receive an instance of my model with its references associations updated. When I try to update de model I do the following:

public void Update(Client updatedClient)
{
    var currentClient = _context.Clientes.Include("Address").Include("Phone").FirstOrDefault(c => c.ClientId == updatedClient.ClientId);
    _context.Entry(currentClient).CurrentValues.SetValues(updatedClient);
}

All the properties of the Client class are updated except for the properties of Address and Phone.

Do I have to do it manually or is there an easier way?

thitemple
  • 5,833
  • 4
  • 42
  • 67

1 Answers1

1

SetValues works only on scalar / complex properties of the entity you pass as the parameter. It doesn't work on navigation properties and it doesn't go deeper into object graph.

You either have to track what changes were made to updatedClients relations and manually set the state of every entity in the graph or you must compare currentClient and updateClient (and all their relations) and update currentClient accordingly (again manually). Deeper explanation of the issue is here.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Don't know if it is the best solution but what I did was: _context.Entry(currentClient.Phone).CurrentValues.SetValues(updatedClient.Phone); _context.Entry(currentClient.Address).CurrentValues.SetValues(updatedClient.Address); – thitemple Apr 11 '12 at 18:11