8

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

Community
  • 1
  • 1
Paul T.
  • 4,938
  • 7
  • 45
  • 93
  • 4
    How do you know it gives no error if you're swallowing the Exception? – D Stanley Nov 14 '12 at 18:37
  • 1
    You should let it error out until you're sure you've got it working. If you have a `try / catch`, then you'll only find out about the exception at the catch block. If you don't have it at all, but you're debugging, you'll instead find about about it on the line that errored, so you can see what's wrong with it. (There are options in VS to change this behavior, but that's the default) – Bobson Nov 14 '12 at 18:49
  • 1
    is your mdf database file been copied to the output directory? maybe it is getting replaced every time you hit F5. does 'int count' always return 1? – Salvador Sarpi Nov 14 '12 at 19:04

1 Answers1

3

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.

Salvador Sarpi
  • 981
  • 1
  • 8
  • 19
  • I tried different combinations, and Copy if newer too. but when I close connection to database and then refresh it, count still = 1 – Paul T. Nov 14 '12 at 19:49
  • what do you mean by refresh it? please use something like this and paste your results: http://pastebin.com/MdwgeHKn – Salvador Sarpi Nov 14 '12 at 20:00
  • Results are simple. If I just press F5 5 times, count = 5. but if I close project and open it and then press F5, count will be = 1. So I think, you are right, database is replaced by new. But how to stop it? – Paul T. Nov 14 '12 at 20:41
  • Just use 'Do not copy' for the mdf file, but be aware of that when you make a change on the mdf. You can also modify your connection string so the paths reach to the mdf file on your project folder and not on the bin folder. something like '../../database.mdf' instead of 'database.mdf' on your app.config – Salvador Sarpi Nov 14 '12 at 20:52