Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add entity-framework to console application (images are included)

I try to add entity-framework to console application: I press "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 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)
            {

            }
        }
    }

It gives no error, but I don't see any changes in database. I described the issue better here

like image 557
Paul T. Avatar asked Nov 14 '12 18:11

Paul T.


People also ask

How do I add Entity Framework to an existing project?

Install Entity FrameworkRight click on your project name and select Manage NuGet Packages. Go to Browse and Select Entity Framework then click Install button to install Entity Framework on your project.

How do I create an Entity Framework in Visual Studio?

Open Visual Studio and create a console project. Go to PROJECT menu -> {project name} Properties.. - and make sure that the project's target framework is . NET Framework 4.5, as shown below.


1 Answers

I did the same steps you did to setup a EF model. your database.mdf file has the Copy to Output Directory set to Copy always, that means that every time you hit F5 (build or debug your app) the file is getting replaced by the empty one on your project.

Changing the Copy to Output Directory on the Properties window of the mdf file should solve your problem.

If you use Copy if newer you are going to be persisting any modifications on the contents of the database until you edit the database (mdf) itself.

With Do not copy any change to the mdf file is not going to get reflected on your application and will probably generate problems with EF.

I recommend for this scenario that you use Copy if newer and fill your basic data in the mdf file so you will have it always available.

like image 55
Salvador Sarpi Avatar answered Nov 15 '22 09:11

Salvador Sarpi