C# 为什么在编辑与EntityFramework的关系时出错?

C# 为什么在编辑与EntityFramework的关系时出错?,c#,entity-framework,linq-to-entities,C#,Entity Framework,Linq To Entities,我有三个EntityFramework(4.3)对象,一个成员、地址和状态。它们看起来像这样: public class Member { public int Id { get; set; } public virtual Address Address { get; set; } /*other properties removed for brevity*/ } public class Address { public int Id { get; set

我有三个EntityFramework(4.3)对象,一个成员、地址和状态。它们看起来像这样:

public class Member
{
    public int Id { get; set; }
    public virtual Address Address { get; set; }
    /*other properties removed for brevity*/
}

public class Address
{
    public int Id { get; set; }
    public virtual State State { get; set; }
    /*other properties removed for brevity*/
}

public class State
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Abbreviation { get; set; }
    public virtual ICollection<Address> Address { get; set; }
}
[HttpPost]
public ActionResult Update(MemberUpdateModel model)
{
    if (!ModelState.IsValid)
    {
        model.SetStateSelectList(_db, model.SelectedStateId);
        return View(model);
    }

    _db.Entry(model.Member).State = EntityState.Modified;
    _db.Entry(model.Member.Address).State = EntityState.Modified;

    // added for address/state relationship
    var selectedState = _db.States.FirstOrDefault(q => q.Id == model.SelectedStateId);
    model.Member.Address.State = selectedState;

    _db.SaveChanges();

    return RedirectToAction("Index");
}
然后我添加地址/状态关系的更新,如下所示:

public class Member
{
    public int Id { get; set; }
    public virtual Address Address { get; set; }
    /*other properties removed for brevity*/
}

public class Address
{
    public int Id { get; set; }
    public virtual State State { get; set; }
    /*other properties removed for brevity*/
}

public class State
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Abbreviation { get; set; }
    public virtual ICollection<Address> Address { get; set; }
}
[HttpPost]
public ActionResult Update(MemberUpdateModel model)
{
    if (!ModelState.IsValid)
    {
        model.SetStateSelectList(_db, model.SelectedStateId);
        return View(model);
    }

    _db.Entry(model.Member).State = EntityState.Modified;
    _db.Entry(model.Member.Address).State = EntityState.Modified;

    // added for address/state relationship
    var selectedState = _db.States.FirstOrDefault(q => q.Id == model.SelectedStateId);
    model.Member.Address.State = selectedState;

    _db.SaveChanges();

    return RedirectToAction("Index");
}
当我们运行上述代码时,会出现以下错误:

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

我做错了什么导致了这种行为?

这里的“乐观并发异常”似乎是一条有意义的线索。可能有用。

我们将更新操作更改为先查询现有数据,然后使用发布的数据更新对象

[HttpPost]
public ActionResult Update(MemberUpdateModel model)
{
    if (!ModelState.IsValid)
    {
        model.SetStateSelectList(_db, model.SelectedStateId);
        return View(model);
    }

    var newModel = new MemberUpdateModel();
    newModel.Member = _db.Members.Include("Address").FirstOrDefault(q => q.Id == model.Member.Id);
    newModel.Member.Address.State = _db.States.FirstOrDefault(q => q.Id == model.SelectedStateId);

    bool success = TryUpdateModel(newModel);
    if (success)
    {
        _db.SaveChanges();
    }
    else
    {
        throw new Exception("TryUpdateModel() Failed");
    }

    return RedirectToAction("Index");
}

这非常有效。

出于好奇,当您将地址更新方法中的EntityStat.Modified行移动到更改后(就在_db.SaveChanges();)之前)会发生什么情况?@Gromer-当我们移动这些行时,错误会消失,但状态根本不会更新。我在谷歌搜索时发现了这一讨论。我们的钥匙都准备好了。我们正在dev机器上进行本地测试。也许我们自己的方法会像答案所暗示的那样阻碍我们,但我不知道在哪里。你能帮忙吗?这条线有必要吗_db.Entry(model.Member).State=EntityState.Modified;也许没有必要同时将该地址和.Address设置为Modified?谢谢分享答案!