I had a problem with Entity Framework code - first in MVC3. I am removing this exception:
An object with the same key already exists in the ObjectStateManager. ObjectStateManager cannot track multiple objects with the same key.
This appeals to SO many times, but I am having problems using any of the suggested solutions in my situation.
Here is a sample code:
FestORM.SaleMethod method = new FestORM.SaleMethod
{
Id = 2,
Name = "Test Sale Method"
};
FestContext context = new FestContext();
string thisQueryWillMessThingsUp =
context.SaleMethods.Where(m => m.Id == 2).Single().Name;
context.Entry(method).State = System.Data.EntityState.Modified;
context.SaveChanges();
EDITED clarify: I'm trying to update an object that already exists in the database.
, . , , , . ObjectStateManager, . , : , ObjectStateManager, .
FWIW, , :
public void Update(T entity)
{
_context.Entry(entity).State = System.Data.EntityState.Modified;
}
public void SaveChanges()
{
_context.SaveChanges();
}
? ...