0

I find it hard to understand how Entity Framework in Visual Studio is dealing with a LocalDB.

In my project root folder I have a .mdf database file, let's call it MyDatabase.mdf

I'm using the MVVM pattern in my WPF application, so in my Model folder, I added an ADO.NET Entity Data Model (code-first from database).

Now in the Server Explorer view I always add a Data Connection to this MyDatabase.mdf, from here I can at least have some control over the database (like perform an EmptyDatabase procedure or whatever).

Now the most single annoying thing is that whenever I run my project, somehow under the Debug folder a new MyDatabase.mdf is created which has NO data in it.

In app.config there is this tag:

<connectionStrings>
    <add name="DysphagiaModel"
         connectionString="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\DysphagiaDB.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

I just can't figure out why Entity Framework works this way, and I find it hard to find any good documentation for developers that are new to this. I'm used to working with PHP and MySQL, which really is a hell lot simpler than this. So this is really extremely frustrating, and I can't imagine I'm the only one having these issues with understanding how Visual Studio (and EF) works and how the underlying mechanics work.

Most articles and documentation out there already assume you know some mechanics, and they won't help you with referencing to what you need to know.

Anyway for this specific issue, how can I have my project use the database I have in my project's root folder?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
QuantumHive
  • 5,613
  • 4
  • 33
  • 55
  • 1
    Since SQL Server (that's what that `.mdf` file and "LocalDB" really are) is a **server-based** approach, you should really put your database **on the server**, create it there, and just access it using the **logical database name** on the server and stop fiddling around with files that get copied around and such stuff.... – marc_s Sep 01 '14 at 10:39
  • 1
    It's not EF but VS. Trust me...this simplifies a lot your development. Each time you build your application all project files (marked to be copied to output) will be copied (also settings will be cleared). This will help you to **run each time a fresh application** (otherwise you may debug/check something from previous run and results may be affected). Also if application crashes leaving inconsistent data your next run will work properly. To avoid that you may mark your DB to be copied only if newer (see file properties). – Adriano Repetti Sep 01 '14 at 10:39

1 Answers1

2

During development Visual Studio creates a copy of the database (and other files) each time it rebuilds your app for debug / release mode. As Adriano posted this will create a new mdf file each time you run a fresh application (It rebuilds your app).

If you wish this not to happen every time, you can change the "Copy to Output" property and choose to ("Do Not Copy" or "Copy if Newer"). To do this you need to open the Solution Explorer, right click on your mdf file, goto properties and change the "Copy to Output Directory".

If you wish to access your data "inserted" on debug mode then you can reference your localdb copy on you bin/debug/[yourdb].mdf on your SQL Server Object Explorer, or create a new data conection (the last one not recommended). But remeber that every time you restart your debug this will start over unless you change your propierties.

waloar
  • 108
  • 1
  • 1
  • 9
  • Looking back 3 years, I see that my knowledge was lacking on this topic. As [marc_s](https://stackoverflow.com/users/13302/marc-s) suggested in the comments, it's wiser to use a database on the server. I can however imagine some rare cases where you want to deploy your database as a local `.mdf` file. – QuantumHive Aug 08 '17 at 08:44