使用SubmitChanges时为什么没有存储数据?

时间:2011-12-01 15:43:10

标签: .net entity-framework linq-to-sql

使用MVC创建Player类型的数据库条目:

[HttpPost]
public ActionResult Create(FormCollection fc, Player player)
{
    players.Players.InsertOnSubmit(player);
    players.SubmitChanges();

    Errors errors;
    if (!IsValid(player, out errors))
    {
        ViewBag.Errors = errors;
        return RedirectToAction("Edit", player);
    }

    return Redirect("/Home/Players");
}

[HttpPost]
public ActionResult Edit(FormCollection fc, Player player)
{
    players.SubmitChanges();

    return Redirect("/Home/Players");
}

我的问题是Edit方法中的players.SubmitChanges()不会更改数据库中的任何内容。在“创建”中使用InsertObSubmit。我可以用另一种方式吗?

2 个答案:

答案 0 :(得分:1)

[HttpPost]
public ActionResult Edit(FormCollection fc, Player player)
{
    // I guess you are forgetting this
    players.Players.AttachAsModified(player)

    players.SubmitChanges();

    return Redirect("/Home/Players");
}

答案 1 :(得分:0)

当您获得播放器值作为编辑操作的输入参数时,您应该首先相应地修改您的players集合 - 这不会自动发生 - 然后才会players.SubmitChanges();