0

i know there are quite a few questions to this error, but i couldnt solve my problem with them.

So i get the error:

InvalidOperationException
An object with the same key already exists in the ObjectStateManager.
The ObjectStateManager cannot track multiple objects with the same key.

I dont even know which key is the same? Can i look that up somehow?

My Controller:

[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();
    }
    //... 
}

My 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; }
}

I would be grateful for youre help.

Mimi Müller
  • 366
  • 3
  • 20
  • You probably want `db.Entry(ViewModel.Changing).State = EntityState.Modified;` but the real problem is that you have a view model that contains properties which are data models (bad practice) –  Mar 28 '16 at 10:16
  • No with that, there is the same error. I didnt know that. How to make it better? – Mimi Müller Mar 28 '16 at 10:21
  • 1
    Then you have other code in the method you have not shown us. But a view model for editing should contain only simple properties to represent what you want to edit in the view, not properties which are data models. When you submit the view model, you get the data model from the database, update its properties and save the data model. Refer [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Mar 28 '16 at 10:25
  • Thank you, sometime you have to see the whole thing. I just focused on this part and didnt recognized that it was influenced by something else. – Mimi Müller Mar 28 '16 at 10:39

1 Answers1

0

Unfortunately i used the Site Model in that Action before like this:

[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();
}
//... 
}

So there was the second key, so the ObjectStateManager couldnt track multiple objects with the same key.

Mimi Müller
  • 366
  • 3
  • 20