错误 ObjectStateManager 中已存在具有相同键的对象。与视图模型
Error An object with the same key already exists in the ObjectStateManager. with ViewModel
我知道这个错误有很多问题,但我无法解决我的问题。
所以我得到错误:
InvalidOperationException
An object with the same key already exists in the ObjectStateManager.
The ObjectStateManager cannot track multiple objects with the same key.
我都不知道哪个键是一样的?我可以查一下吗?
我的控制器:
[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不能用同一个键跟踪多个对象。
我知道这个错误有很多问题,但我无法解决我的问题。
所以我得到错误:
InvalidOperationException
An object with the same key already exists in the ObjectStateManager.
The ObjectStateManager cannot track multiple objects with the same key.
我都不知道哪个键是一样的?我可以查一下吗?
我的控制器:
[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不能用同一个键跟踪多个对象。