I can add an entity but when I try to update Entity Framework doesn't work. This is what I'm doing:
Get model in action filter in Web API. I lookup some data about this model in the database and use that to change/hydrate different parts of the model. Yes, it has to be exactly that way.
Controller runs. Controller does nothing except pass the model to the repository to be saved.
Repository checks if a model with that ID exists in the database. If it does it tries to attach and then save.
Code:
_db.Airplanes.Attach(airplane);
// _db.Entry(airplane).State = EntityState.Modified; // doesn't do anything
_db.SaveChanges();
It doesn't work. Instead, it throws this error message:
System.Data.Entity.Core.OptimisticConcurrencyException: 'Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.'
Perhaps the problem is that I originally got the entity way back in the Action Filter and it has since gone out of context or something? In any event I don't know why Entity Framework won't let me just add it back.
Edit: I'm being forced to edit this to explain how this isn't solved by another question. ...umm, it isn't. The burden of proof should be on the person making the claim.