I can't save data using Entity Framework

余生颓废 提交于 2019-12-13 16:15:41

问题


I use this code to add one item to a SpareParts table in my database.

try
{
    Database1Entities _db = new Database1Entities();

    SparePart sparePart = new SparePart();
    sparePart.manufactureID = 2;
    sparePart.name = "detail";

    _db.SpareParts.AddObject(sparePart);

    int changesNumber = _db.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);

    int count = _db.SpareParts.Count();
    int i = 8 + 1;
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}

I tried _db.SaveChanges(). That didn't help.

I don't get any exceptions. And if I start debug 4 times, I will see count = 4. And I can iterate through all my items. But if I wait 1 minute and start debugging one more time, I see count = 1. So it seems that my changes are still not in database.

来源:https://stackoverflow.com/questions/13381738/i-cant-save-data-using-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!