【问题标题】:Receiving an attempt was made to remove a relationship between x and x however one of the relationship's foreign keys尝试删除 x 和 x 之间的关系,但是关系的外键之一
【发布时间】:2012-08-30 08:42:47
【问题描述】:

我有一个 MVC 项目,我有一个需要同时更新父实体和多个子实体的情况。在发布操作中,我收到“尝试删除 x 和 x 之间的关系,但是关系的外键之一”,这很奇怪,我所做的只是更新,我没有删除任何实体。我正在使用 Linq to SQL 和 MVC3。伪代码如下:

@model Project.Models.ParentModel

...

@using (Html.BeginForm()) {
@Html.Label("Parent property")
@Html.EditorFor(model => model.ParentProperty)

@foreach (var child in Model.Childs)
{
Html.RenderPartial("_EditChild", child)
// Which is nothing more than a label and an editor for a property like:
// @model Project.Models.ChildModel
// @Html.Label("Child property")
// @Hteml.EditorFor(model => model.ChildProperty)
}

...

}

动作看起来像:

public ActionResult Edit(int id, FormCollection collection)
{

var parent = new Parent();
TryUpdateModel(Parent()); // which updates the parent and the child properties correctly
dataContext.SubmitChanges();

}

谁能解释这种行为。再一次,我不会删除或删除任何子实体!

【问题讨论】:

  • 我在局部视图中编辑时遇到了一些问题。您可以尝试不使用局部视图(编辑器直接在 foreach 中而不是局部呈现)?
  • 我用模板编辑器试过了,但同样的错误!
  • 我也试过这个解决方案stackoverflow.com/questions/8894442/…,但是模型绑定器不会绑定子实体。

标签: asp.net-mvc-3 linq-to-sql one-to-many


【解决方案1】:

对列表的绑定可能非常讨厌,我自己也遇到了一些问题。我修改了我的列表编辑代码以与孩子一起使用并对其进行了测试,它可以正常工作并且数据已正确绑定并在发布操作中可见:

@model MvcApplication2.Models.Parent

@using (Html.BeginForm())
{
    <table>
        @{
                <tr>
                    <td>
                        @Html.TextBoxFor(m => m.Text)
                        @Html.HiddenFor(m => m.ID)
                    </td>
                </tr>
                for (int i = 0; i < Model.Children.Count; i++)
                {
                            <tr>
                                <td>
                                    @Html.TextBoxFor(x => x.Children[i].Title)
                                    @Html.HiddenFor(x => x.Children[i].ID)
                                </td>
                            </tr>
                }
        }
    </table>
    <div class="button">
        <input class="submit" type="submit" name="btnSave" id="btnSave" value="Save" />
    </div>
}

我的测试控制器如下所示:

[HttpGet]
public ActionResult EditingChildren()
{
    Parent parent = new Parent() { Text = "" };
    parent.Children = new List<Child>();
    parent.Children.Add(new Child() { Title = "" });
    parent.Children.Add(new Child() { Title = "" });
    parent.Children.Add(new Child() { Title = "" });
    return View(parent);
}

[HttpPost]
public ActionResult EditingChildren(Parent parent)
{
    // save parent with children
}

编辑我关于使用 linq to sql 保存数据的帖子:

如果你没有在视图上绑定 ID,它会在 post 方法的对象中留空。这会给您保存数据带来麻烦。 因此,我通常将 ID 绑定到一个隐藏字段,这样它就不会再为空了(查看上面编辑的代码并在 TextBoxFor 下添加了 HiddenFor)。

还可以在此网站上查看有关使用 linq to sql 更新数据的信息:

http://davedewinter.com/2009/04/07/linq-to-sql-updating-entities/(在附加实体、更改属性、更新下)

还有这篇文章: enter link description here

【讨论】:

  • 您能告诉我您如何处理 POST 操作中的更新吗?我大部分时间都使用 TryUpdateModel() 方法,但在这种情况下,它使子 ID 为 0
  • 要保存对象,您必须在父 linq to sql 对象上使用 Attach() 方法,然后使用 SubmitChanges()
  • 这行不通,因为实体已经存在于数据库中!
  • 我通常将视图上的 ID 属性绑定在隐藏字段中,因为它将被回发。我已经编辑了我的帖子,希望对您有所帮助。
  • 是的,这就是我所做的,但没有运气。奇怪的是,即使它没有正确更新 EntitySet,它也会更新 List。所以我不得不以丑陋的方式来做,在列表中迭代并从数据库中获取行来更新属性。
猜你喜欢
  • 2015-11-04
  • 2019-09-08
  • 2012-10-18
  • 2013-02-16
  • 2019-01-25
  • 2013-07-27
  • 2019-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多