How to add entity frames to the console application (images are included)

I am trying to add an entity-framework for a console application: I click "add new item" and enter image description here

then enter image description here

then

enter image description here

enter image description here

enter image description here

enter image description here

then I added the code:

class Program { static void Main(string[] args) { try { Database1Entities db = new Database1Entities(); db.AddToTableTest(new TableTest { name = "name" }); db.SaveChanges(); int count = db.TableTest.Count(); int ui = 9 + 0; } catch (Exception e) { } } } 

This does not give any errors, but I do not see any changes to the database. I described the problem better here

+6
source share
1 answer

I took the same steps as you to customize the EF model. your database.mdf file has Copy to Output Directory set to Copy always , which means that every time you press F5 (create or debug an application), the file is replaced by an empty one in your project.

Changing Copy to Output Directory in the mdf file properties window should fix your problem.

If you use Copy if newer , you will save any changes to the contents of the database until you change the database (mdf) yourself.

With Do not copy any change to the mdf file will not affect your application and is likely to cause problems with EF.

I recommend using Copy if newer for this scenario and filling in your main data in the mdf file so that you are always available.

+3
source

All Articles