簡體   English   中英

錯誤ObjectStateManager中已經存在具有相同鍵的對象。 使用ViewModel

[英]Error An object with the same key already exists in the ObjectStateManager. with ViewModel

我知道這個錯誤有很多問題,但是我無法解決他們的問題。

所以我得到了錯誤:

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無法使用同一鍵跟蹤多個對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM