Wow, lots of answers, but I got this error when I did something slightly different that no on else has mentioned.
Long story short, if you create a new object and tell EF that its modified using the EntityState.Modified
then it will throw this error as it doesn't yet exist in the database. Here is my code:
MyObject foo = new MyObject()
{
someAttribute = someValue
};
context.Entry(foo).State = EntityState.Modified;
context.SaveChanges();
Yes, this seems daft, but it arose because the method in question used to have foo
passed to it having been created earlier on, now it only has someValue
passed to it and creates foo
itself.
Easy fix, just change EntityState.Modified
to EntityState.Added
or change that whole line to:
context.MyObject.Add(foo);