Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Not Saving / Stop database being rebuilt

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.
like image 950
Brett Avatar asked Nov 17 '25 15:11

Brett


1 Answers

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.

like image 108
Brett Avatar answered Nov 19 '25 05:11

Brett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!