1

I post a viewmodel to my controller and I automap it to the entity object and I persist it with the entry() method changing the state of it depending if it is to add or update. I made sure that the objects wasn't null after I automap it, maybe I'm persisting it wrong. My problem is that the nested objects in the does not persist the changes.

public ActionResult saveUpdatePessoa(PessoaViewModel id)
    {

        if (ModelState.IsValid)
        {
            var pes = MvcApplication.Mapper.Map<PessoaViewModel, Pessoa>(id);
            using (var db = new CityManagerDbCtx())
            {                   

                db.Entry(pes).State = pes.codigo == 0 ? EntityState.Added : EntityState.Modified;
                //db.Entry(pes.endereco).State = pes.endereco.codigo == 0 ? EntityState.Added : EntityState.Modified;//Gives me error here if I uncomment this line

                db.SaveChanges();

                string smsg = id.codigo > 0 ? "Pessoa editada com sucesso." : "Pessoa salva com sucesso.";
                ModelState.Clear();
                return Json(new SubmitFormViewModel
                {
                    success = true,
                    form = RenderHelper.PartialView(this, PESSOA_FORM_VIEW, new PessoaViewModel { dt_cad = DateTime.Now.Date, dt_nasc = DateTime.Now.Date }),
                    successMessages = new string[] { smsg },
                    errorMessages = new string[] { }
                });
            }                              
        }

        return Json(new SubmitFormViewModel
        {
            success = false,
            form = RenderHelper.PartialView(this, PESSOA_FORM_VIEW, id),
            successMessages = new string[] { },
            errorMessages = ModelState.Values.SelectMany(m => m.Errors).Select(e => e.ErrorMessage).ToArray()
        });
    }
viniciusalvess
  • 756
  • 8
  • 18
  • Maybe upload the PessoaViewModel and Pessoa model so we can see the nested objects. If you have updated these nested objects (not referenced another, actually changed the values of the nested objects) you will need to explicitly update them as you did the the 'Pessoa' class. It won't implicitly know to update these nested classes just because the wrapper class was updated. – Norman Bentley Feb 17 '16 at 00:49

1 Answers1

1

If these 'nested objects' are classes, they will need to be explicitly updated just like the "Pessoa" class. It won't implicitly know to update these nested classes just because the wrapper class was updated.

However, adding a class with nested objects will add everything. It is only different for updates.

Norman Bentley
  • 640
  • 5
  • 20
  • they are classes ! Do you mean that I'll have to do the entry() method setting the state of it to modified for every object that is composited in the classe when I want to update it ? – viniciusalvess Feb 17 '16 at 01:13
  • sadly so, here is a related question https://stackoverflow.com/questions/13236116/entity-framework-problems-updating-related-objects – Norman Bentley Feb 17 '16 at 01:16
  • Tried to do the exaple you sent me , but I'm getting and InvalidOperationException saying: "The property 'codigo' is part of the object's key information and cannot be modified." the exception happens on this line: db.Entry(dbFoo.endereco).CurrentValues.SetValues(pes.endereco); – viniciusalvess Feb 17 '16 at 01:38
  • Are your id's currently null for the nested objects? Does it help if you set the 'id' value of the nested classes to their db values before updating them? – Norman Bentley Feb 17 '16 at 01:41
  • If I hard code include the objects and make sure that the entry object key and the object key that I pass to the setValues are the same , it works ! I feel a little disapointed because I thought it would update automatically the related objects. But thanks a lot @Norman Bentrley. I'm new to asp.net and I have getting a ton of exceptions hehe. – viniciusalvess Feb 17 '16 at 02:10
  • I faced this same exact issue last month. I had like 12 subclasses (nightmare). No probs ;) – Norman Bentley Feb 17 '16 at 02:12
  • And as the software grows , we have to be comming back through the whole code to add the new fields. I'm disappointed with Entity Framework after knowing this ! – viniciusalvess Feb 17 '16 at 02:19