views:

1898

answers:

4

I have an entity that has one child and I need to update it, but, in the TryUpdateModel method, it does not accept an strong typed object (accepts only a FormCollection), and when I try to update it, I get the following error.

{"A relationship is being added or deleted from an AssociationSet 'FK__SG_Usuari__ID_Si__534D60F1'. With cardinality constraints, a corresponding 'SG_Usuario' must also be added or deleted."}

The problem is that I can't load in the formcollection the child property, only an id, but not the entire object.

A: 

You can directly click update from model in entity framework and it will automatically update the enitty with all the relationship

jalpesh
A: 

I'm not updating the model, I'm updating the data in it, but anyways, thanks!

A: 

the "create" statement is like this:

    public ActionResult Edit(FormCollection  form)
    {

      Usuario usuario = new Usuario
             {
                 NomeUsuario = form["Usuario.NomeUsuario"],
                 IdeUsuario = form["Usuario.IdeUsuario"],
                 NumRegistroRM = form["Usuario.NumRegistroRM"],
                 SenUsuario = form["Usuario.SenUsuario"],
                 SituacaoUsuario = this.SituacaoUsuarioService.GetSituacaoUsuario(x => x.ID_SituacaoUsuario == Convert.ToInt32(form["Usuario.SituacaoUsuario"]// note that i have to retrieive the entire object... the "child"

             };

      this.UsuarioService.AddUsuario(usuario);
     }

the edit statement should be like this:

 TryUpdateModel(a, "Usuario", this.GetUsuarioWhiteList(), form.ToValueProvider()); // but the form contains only the id and I can't load the child in it nor pass the object.
A: 

I had the same issue recently and I managed to solve it when I changed the cardinality ratios for my foreign keys in the child table from 1:many to 0..1:many in the Entity Designer and it worked fine.

Vasco