简体   繁体   中英

Entity framework, changes not saved

I'm encountering a problem that I've never seen. Everything worked perfectly before and from today my code doesn't save some updates.

My code is in t_inscription.cs

public void emailsent(t_inscriptions inscription = null)
{
    if (inscription == null)
    {
        inscription = this;
    }

    inscription.id_etat_inscription = 5;
    db.AcceptAllChanges();
    db.SaveChanges();
}

When I debug , the app goes through this code, the object inscription is not null and correctly loaded, its id_etat_inscription turns to 5, and I receive no error message.

But then when I go to my db, I don't have no inscription with this id_etat_inscription at 5.

Note that it's a foreign key linked with a table "t_etats_inscriptions" containing ids froms 1 to 6.

Am I missing something?

Thanks a lot=)

Try this instead

db.Entry<t_inscriptions >(inscription ).State = EntityState.Modified;
db.SaveChanges();  

or

db.Entry(t_inscriptions).State = EntityState.Modified;
db.SaveChanges(); 

I'll put it here cause it's "a solution to the problem",

When I replace my code by

public void emailsent(t_inscriptions inscription = null)
{
    if (inscription == null)
    {
        inscription = this;
    }

    t_inscriptions inscription2 = db.t_inscriptions.FirstOrDefault(x => x.id == inscription.id);
    inscription2.id_etat_inscription = 5;

    inscription.id_etat_inscription = 5;
   //db.Entry<t_inscriptions>(inscription).State = EntityState.Modified;
 //   db.t_inscriptions.Attach(inscription);
   db.ObjectStateManager.ChangeObjectState(inscription2, EntityState.Modified);
  //  db.AcceptAllChanges();
    db.SaveChanges();
}

It works :/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM