Asp.net mvc 实体框架-使用子属性更新实体

Asp.net mvc 实体框架-使用子属性更新实体,asp.net-mvc,entity-framework,linq-to-entities,entity,Asp.net Mvc,Entity Framework,Linq To Entities,Entity,我有一个实体有一个子实体,我需要更新它,但是在TryUpdateModel方法中,它不接受强类型对象,只接受FormCollection,当我尝试更新它时,我得到以下错误 {正在从AssociationSet'FK_usg_Usuari_uid_Si_u534D60F1'添加或删除关系。使用基数约束,还必须添加或删除相应的'SG_Usuario' 问题是我无法在formcollection中加载子属性,只能加载一个id,而不能加载整个对象。您可以直接单击实体框架中的“从模型更新”,它将自动更新具

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

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


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

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

create语句如下所示:

    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.
编辑语句应如下所示:

    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.

我最近也遇到了同样的问题,当我将子表中外键的基数比率从1:many更改为0..1:many时,我成功地解决了这个问题。在实体设计器中,它工作得很好。

请显示导致问题的代码,说明它是否发生在运行时或设计时,并发布完整的异常,具有所有InnerException和堆栈跟踪。发布ex.ToString的结果