简体   繁体   English

实体框架,更改未保存

[英]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 我的代码在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. 当我调试时,应用程序将通过此代码,对象题词不为null且未正确加载,其id_etat_inscription变为5,并且我未收到任何错误消息。

But then when I go to my db, I don't have no inscription with this id_etat_inscription at 5. 但是当我进入数据库时​​,在5点我没有这个id_etat_inscription的题词。

Note that it's a foreign key linked with a table "t_etats_inscriptions" containing ids froms 1 to 6. 请注意,这是与表“ t_etats_inscriptions”链接的外键,该表包含ID从1到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 :/ 有用 :/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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