C# 错误ObjectStateManager中已存在具有相同密钥的对象。使用ViewModel
我知道这个错误有很多问题,但我无法用它们解决我的问题 所以我得到了一个错误: 无效操作异常C# 错误ObjectStateManager中已存在具有相同密钥的对象。使用ViewModel,c#,asp.net-mvc,viewmodel,invalidoperationexception,objectstatemanager,C#,Asp.net Mvc,Viewmodel,Invalidoperationexception,Objectstatemanager,我知道这个错误有很多问题,但我无法用它们解决我的问题 所以我得到了一个错误: 无效操作异常 ObjectStateManager中已存在具有相同密钥的对象 ObjectStateManager无法跟踪具有相同密钥的多个对象 我甚至不知道哪把钥匙是一样的?我能查一下吗 我的控制器: [HttpPost] public ActionResult Meeting(ViewModel ViewModel) { var ALL = db.Sites.Where(p => p.Content.
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();
}
//...
}
[HttpPost]
公共行动结果会议(ViewModel ViewModel)
{
var ALL=db.Sites.Where(p=>p.Content.Any(a=>a.Date.CompareTo(DateTime.Now)l.Customer.Service).ToList();
//添加arnt由用户添加的信息
ViewModel.Changing.LastUpdate=DateTime.Now;
ViewModel.Changing.LastUpdaterId=UpdaterID;
站点当前=视图模型。正在更改;
if(ModelState.IsValid)
{
db.Entry(current.State=EntityState.Modified;//下面是错误
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; }
}
公共类视图模型
{
公共管理工具.Models.Site更改{get;set;}
public int[]可用已选择{get;set;}
public int[]RequestedSelected{get;set;}
公共字符串SavedRequested{get;set;}
公开列表开放问题{get;set;}
公共列表关闭dissue{get;set;}
公共管理工具.Models.Site站点{get;set;}
公共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();
}
//...
}
[HttpPost]
公共行动结果会议(ViewModel ViewModel)
{
//如果未注释掉以下部分,则会出现错误-->
//var ALL=db.Sites.Where(p=>p.Content.Any(a=>a.Date.CompareTo(DateTime.Now)l.Customer.Service).ToList();
//添加arnt由用户添加的信息
ViewModel.Changing.LastUpdate=DateTime.Now;
ViewModel.Changing.LastUpdaterId=UpdaterID;
站点当前=视图模型。正在更改;
if(ModelState.IsValid)
{
db.Entry(current.State=EntityState.Modified;//下面是错误
db.SaveChanges();
}
//...
}
因此有了第二个键,ObjectStateManager无法使用同一个键跟踪多个对象。您可能需要
db.Entry(ViewModel.Changing).State=EntityState.Modified;
但真正的问题是,您的视图模型包含的属性是数据模型(错误的做法)不,还有同样的错误。我不知道。如何使它更好?然后在您没有向我们展示的方法中有其他代码。但是用于编辑的视图模型应该只包含简单的属性来表示您要在视图中编辑的内容,而不是数据模型的属性。提交视图模型时,您将获得数据模型从数据库中,更新其属性并保存数据模型。谢谢,有时您必须了解整个情况。我只关注这一部分,没有意识到它受到其他因素的影响。