Entity Framework Not Saving / Stop database being rebuilt

て烟熏妆下的殇ゞ 提交于 2019-12-07 14:51:29

问题


I am teaching myself about the entity framework using a tutorial found at http://msdn.microsoft.com/en-us/data/jj591506. I am working with the code below.

using (var db = new DatabaseEntities())
{
        var section = new Section
        {
            SectionID = 1,
            SectionName = "Bob"
        };
        db.Sections.Add(section);
        db.SaveChanges();
}

For some reason this code is not writing to my database. No error are being thrown. Do you have ideas what I could look at or what could be the cause? This is an almost exact copy of the example give on the MSDN page linked above.

This problem looks very similar to what is happening in this question: Entity Framework not actually saving changes unfortunately the person who answered this question did not explain how they got resolved the problem. They explained that they checked the SQL going over to the database, and it was fine, after some investigation, turned out that they had a copy of the database within the project, which was over-riding the one in the output when it was rebuilt. My problem is I don't know how to stop this occurring. Can you help?

I have also looked at:

  • Entity framework saving context
  • Entity Framework: Attached Entities not Saving
  • many pages from Google.

回答1:


So I end up taking the advice above an creating a new project unfortunately that did not work. 10 hours of Googling later and I found the solution. I a suspected the database was being rebuilt.

To prevent a database being rebuilt right click on it in the solution explorer and go to properties. Under properties find the property Copy to output directory and change the value to Do not copy.

This actually broke my code because the database was not in the output directory. To get the program to work again I had to manually put a copy of the database in my debug directory. For my project. In Windows 8 with Visual Studio 2012 this was located at:

%UserProfile%\Documents\Visual Studio 2012\Projects\PROJECTNAME\PROJECTNAME\bin\Debug

Hope this saves others searching the Internet for hours on end.



来源:https://stackoverflow.com/questions/14090819/entity-framework-not-saving-stop-database-being-rebuilt

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