Entity Framework Db.SaveChanges() not working?

PsyGnosis picture PsyGnosis · Jul 9, 2014 · Viewed 10.6k times · Source

Can u tell me what is the problem?

can u tell me what's the problem

Answer

Liviu Mandras picture Liviu Mandras · Jul 9, 2014

If you are using two different instances of the DbContext (the db variable as you named it) then nothing will be saved when you call SaveChanges on a context different than the one where your entities are tracked. You need to use the Attach method first.

db.customer_images.Attach(item);
db.SaveChanges();

However I think in your case you can avoid the attach step if you refactor a bit you code and don't use the DbContext from the entity itself.