none
How do I Create an ADO.NET EDT for a database and add rows with C#? RRS feed

  • Question

  • So far I have added a service based Database to the solution. I made a simple table in it. I created an ADO.NET Entity Data Model that was connected to that database with that table checkmarked. Then I use this code:

     private void button1_Click(object sender, EventArgs e)
            {
                var db = new Database1Entities1();
                var st = new Student { StudentID = 1, StudentName = "Jack" };
                 
                db.Students.Add(st);              
                db.SaveChanges();
    
                foreach (Student s in db.Students)
                    listBox1.Items.Add(s.StudentName);
          
            }
    It works so long as the program is running. Then I exit, and the row I added is not in the table data. I have searched long and hard and no one seems to have a solution to this seemingly basic problem, and I'm not sure what is wrong.
    • Moved by CoolDadTx Monday, September 14, 2015 5:03 PM EF related
    Monday, September 14, 2015 2:59 PM

All replies

  • Maybe the db file is read only. Maybe you have the db file in a location like C:\Program Files where it can't be written to.
    Monday, September 14, 2015 8:42 PM
  • Hi Jack,

    From you description, student object was added to EF but not in database. To investigate this strange problem, please try to use Sql Server Profiler. This tool can monitor T-SQL statements sent by EF, using this tool we could know whether the insert statement was inserted into database.

    Tool: https://msdn.microsoft.com/en-us/library/ms181091.aspx

    How to use: http://www.codeproject.com/Articles/21371/SQL-Server-Profiler-Step-by-Step

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 15, 2015 1:35 AM
    Moderator
  •  

    varst = new Student { StudentID = 1, StudentName = "Jack" };

    Also, I will add that you setting StudentID = 1 if you are using StudentID as the primary key in the table and the key is an auto incremented primay key, by you setting it to 1 tells if EF to try to do an update of an existing record in the table that has an StudentID = 1. A StudentID = 0 tells EF to do an insert/add of a table record in the table, and MS SQL Server will add 1 to the next available auto incremented number for the primary key for the table, which will be populated back to the StudentID  Student entity at insert time.

    Tuesday, September 15, 2015 2:27 AM
  • Are you sure you are checking on the correct database? Check the app.config file database connection string.

    Happy Coding. 

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Tuesday, September 15, 2015 5:59 AM
  • Hello JackEMooreIII,

    >>It works so long as the program is running. Then I exit, and the row I added is not in the table data

    I guess there would be a copied .mdf file in the bin folder and the data is saved into this file, please check it. And if so, you could change the settings of the Copy to Output Directory property of the .mdf file to be Do not copy and then run your app again.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 16, 2015 1:31 AM
    Moderator