0

我知道这个错误有很多问题,但我无法解决我的问题。

所以我得到了错误:

InvalidOperationException
ObjectStateManager 中已存在具有相同键的对象。
ObjectStateManager 无法跟踪具有相同键的多个对象。

我什至不知道哪个键是一样的?我可以以某种方式查找吗?

我的控制器

[HttpPost]
public ActionResult Meeting(ViewModel ViewModel)
{
    var ALL = db.Sites.Where(p => p.Content.Any(a => a.Date.CompareTo(DateTime.Now) <= 0)).OrderBy(l => l.Customer.Service).ToList();

    //Adding informations that arnt added by user
    ViewModel.Changing.LastUpdate = DateTime.Now;
    ViewModel.Changing.LastUpdaterId = UpdaterID;
    Site current = ViewModel.Changing;


    if (ModelState.IsValid)
    {
        db.Entry(current).State = EntityState.Modified; //Here is the error
        db.SaveChanges();
    }
    //... 
}

我的视图模型

public class ViewModel
{
    public managementtool.Models.Site Changing { get; set; }
    public int[] AvailableSelected { get; set; }
    public int[] RequestedSelected { get; set; }
    public string SavedRequested { get; set; }
    public List<managementtool.Models.Issue> OpenIssue { get; set; }
    public List<managementtool.Models.Issue> ClosedIssue { get; set; }
    public managementtool.Models.Site Site { get; set; }
    public int ID { get; set; }
}

我会很感激你的帮助。

4

1 回答 1

0

不幸的是,我之前在该操作中使用了站点模型,如下所示:

[HttpPost]
public ActionResult Meeting(ViewModel ViewModel)
{
//The Error appears if the following part isnt commented out -->
//var ALL = db.Sites.Where(p => p.Content.Any(a => a.Date.CompareTo(DateTime.Now) <= 0)).OrderBy(l => l.Customer.Service).ToList();


//Adding informations that arnt added by user
ViewModel.Changing.LastUpdate = DateTime.Now;
ViewModel.Changing.LastUpdaterId = UpdaterID;
Site current = ViewModel.Changing;


if (ModelState.IsValid)
{
    db.Entry(current).State = EntityState.Modified; //Here is the error
    db.SaveChanges();
}
//... 
}

所以有第二个键,所以 ObjectStateManager 不能用相同的键跟踪多个对象。

于 2016-03-28T10:47:02.220 回答