我知道这个错误有很多问题,但我无法用它们解决我的问题。
所以我得到了错误:
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();
}
//...
}
我的ViewModel
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; }
}
如果你能帮助我,我将不胜感激。
不幸的是,我之前在该操作中使用了站点模型,如下所示:
[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不能用同一个键跟踪多个对象。