none
Unable to Update .mdf using Entity Framework RRS feed

  • Question

  • I am trying to insert Data in an .mdf file using Entity Framework but there is no data saved in the database. (I am using VS 2013)

    Code against the button is

    private void BtnSubmit_Click(object sender, RoutedEventArgs e)
            {
                Product record = new Product();
                record.ProductName = txtProductName.Text;
                AzadIndustryEntities1 Db = new AzadIndustryEntities1();
                Db.Products.Add(record);
                Db.SaveChanges();
                MessageBox.Show("Record Inserted");
            }


    samEE


    • Edited by Sameel Nawaz Monday, January 26, 2015 6:55 PM
    • Moved by Caillen Tuesday, January 27, 2015 5:13 AM
    Monday, January 26, 2015 5:32 PM

Answers

  • Hello Sameel,

    From your description, the code should run without any problem, however, the data is just not shown in database. From my experience, this scenario usually occurs because there is a copied .mdf file and the data is saved into this file, and it is usually stored in bin folder. Please check if you are under this scenario.

    Regards,

    Fred.


    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.

    • Marked as answer by Sameel Nawaz Saturday, January 31, 2015 12:23 PM
    Tuesday, January 27, 2015 9:58 AM
    Moderator
  • My problem is solved. Going to explain it so that it could help others also.

    As the default property of .mdf file Copy to Output Directory is Copy always so when we debug our program a copy of the .mdf file is copied in the debug folder which is in the bin folder  (Select Show All Files in Solution Explorer to view the bin folder) so, whatever changes we make in database through code it is saved in the copied .mdf that is in the debug folder. When we debug our program again the same steps are performed again and the previous database is overwritten. To prevent such happening the property of .mdf file mentioned above should be set to Copy if newer so, if there is any change in the model only then the .mdf will be overwritten


    samEE

    • Marked as answer by Sameel Nawaz Saturday, January 31, 2015 12:24 PM
    Saturday, January 31, 2015 12:24 PM

All replies

  • Hello Sameel,

    From your description, the code should run without any problem, however, the data is just not shown in database. From my experience, this scenario usually occurs because there is a copied .mdf file and the data is saved into this file, and it is usually stored in bin folder. Please check if you are under this scenario.

    Regards,

    Fred.


    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.

    • Marked as answer by Sameel Nawaz Saturday, January 31, 2015 12:23 PM
    Tuesday, January 27, 2015 9:58 AM
    Moderator
  • My problem is solved. Going to explain it so that it could help others also.

    As the default property of .mdf file Copy to Output Directory is Copy always so when we debug our program a copy of the .mdf file is copied in the debug folder which is in the bin folder  (Select Show All Files in Solution Explorer to view the bin folder) so, whatever changes we make in database through code it is saved in the copied .mdf that is in the debug folder. When we debug our program again the same steps are performed again and the previous database is overwritten. To prevent such happening the property of .mdf file mentioned above should be set to Copy if newer so, if there is any change in the model only then the .mdf will be overwritten


    samEE

    • Marked as answer by Sameel Nawaz Saturday, January 31, 2015 12:24 PM
    Saturday, January 31, 2015 12:24 PM