1

我有一个实体有一个孩子,我需要更新它,但是在 TryUpdateModel 方法中,它不接受强类型对象(只接受 FormCollection),当我尝试更新它时,我收到以下错误.

{“正在从 AssociationSet 'FK__SG_Usuari__ID_Si__534D60F1' 中添加或删除关系。使用基数约束,还必须添加或删除相应的 'SG_Usuario'。”}

问题是我无法在 formcollection 中加载子属性,只能加载一个 id,而不是整个对象。

4

3 回答 3

0

“创建”语句是这样的:

    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);
     }

编辑语句应该是这样的:

 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.
于 2009-07-03T13:13:11.293 回答
0

您可以直接在实体框架中单击从模型更新,它会自动更新具有所有关系的实体

于 2009-07-03T07:20:00.823 回答
0

我最近遇到了同样的问题,当我在实体设计器中将子表中外键的基数比从 1:many 更改为 0..1:many 时,我设法解决了这个问题,并且效果很好。

于 2009-12-07T10:08:39.273 回答