Entity Framework Db.SaveChanges() not working?

14,059

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.

Share:
14,059
PsyGnosis
Author by

PsyGnosis

Game Developer at Gamolt Game Studio

Updated on June 06, 2022

Comments

  • PsyGnosis
    PsyGnosis almost 2 years

    Can u tell me what is the problem?

    can u tell me what's the problem

    • Liviu Mandras
      Liviu Mandras almost 10 years
      what do you mean "is not working"? what is the exception? what EF version are you using? how do you pass the db variable to the Customer entity and to the click handler in the aspx page?
    • phil soady
      phil soady almost 10 years
      can you tell us what the error is ? My Crystal ball is in for service
    • PsyGnosis
      PsyGnosis almost 10 years
      I am not getting an error. it's not save the values
    • PsyGnosis
      PsyGnosis almost 10 years
      db variable in the same class like entity db = new entity(); and also aspx page has different db variable. I think its not working because of that