1

I have MDF file which i use as DB and connecting to it using Linq-to-SQL.

my connection string is:

<add name="TasteTeam.Properties.Settings.TasteDBConnectionString" connectionString="Data Source=(LocalDB)\v11.0; AttachDbFilename=|DataDirectory|\TasteDB.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

my problem is that when i run the program and adds data to a table it adds into the MDF file that in the bin folder instead of the main MDF file.

this situation causes the MDF file to be empty every time i restart myprogram instead of persisting the data.

what can i do to make both MDF and the tmp MDF file in the bin be the same file (i already tried all the "copy to output directory" options but nothing seem to help)

Jynxed
  • 19
  • 6
  • http://stackoverflow.com/questions/17147249/why-saving-changes-to-a-database-fails/17147460#17147460. – Steve May 21 '16 at 14:52
  • @Steve I looked at your answer and it is indeed very helpfull but can you please explain couple of things that were not clear enough for me? 1. by creating 2 connection strings how can i keep both MDF files persistent? 2. is there any way to choose only 1 mdf fie to work with (i prever not the one in the BIN folder because it gets deleted everytime) – Jynxed May 21 '16 at 15:16
  • Change the property Copy To OutputDirectory to Copy Newer. This will block Visual Studio from copying from project folder to the working folder. Otherwise use the proposed solution from marc_s below. This is how DataDirectory works – Steve May 21 '16 at 16:33

1 Answers1

1

The whole AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

The real solution in my opinion would be to

  1. install SQL Server Express (and you've already done that anyway)

  2. install SQL Server Management Studio Express

  3. create your database in SSMS Express, give it a logical name (e.g. TasteDB)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

    Data Source=.\\SQLEXPRESS;Database=TasteDB;Integrated Security=True
    

    and everything else is exactly the same as before...

Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Thanks for the answer. what if I have to work with the AttachDbFileName approach and still want my data to be shown in the main MDF file? my inserts are working perfectly but as you mentioned they are added to different mdf file and once i rebuild the project then my data is gone (because the temporary mdf file in bin folder is gone) – Jynxed May 21 '16 at 15:01