tesla1060
tesla1060

Reputation: 2765

Where to put the local database in a Visual Studio solution?

In my solution I have four projects

  1. Views

  2. ViewModels

  3. Models

  4. DataAccess

As the name suggested, DataAccess will be responsible to communicate with my local database, currently a mdf file. My question is where should I put this file?

Should I put in the bin\debug folder of DataAccess project or should I put in \bin\debug folder of Viewproject as this project is the start up project and where the exe file exists eventually.

====================================================

I am trying to created a wpf mvvm desktop application,

I am also using Entity Framework Code First, so I would like to put my connectionStrings in my App.config file which is currently under Views project. How will I specify my connection string then?

Upvotes: 1

Views: 1808

Answers (3)

Thiru
Thiru

Reputation: 13

If you're creating a MVC application you can find App_Data folder. You can keep the local db here. Or create App_Data folder in your project and add local db.

Also you can keep the connection string in Web.Config file in your project.

Upvotes: 1

Clark Kent
Clark Kent

Reputation: 315

Maybe you should have a look at the App_Data folder? More information here: http://www.w3schools.com/aspnet/mvc_folders.asp

I would put it here because it is not accessible to your clients via the browser and because the name of the folder suggests that this is the place where the application data is stored.

Hope this helps! Good luck!

Upvotes: 0

Matt Searles
Matt Searles

Reputation: 2765

Don't put it in your bin folder, that's where source code goes after it's compiled. You should be able to delete the bin and obj folders and recreate them by recompiling the project.

DataAccess/db.mdf seems like a pretty reasonable location. When the project is published, you won't have a directory structure the same as your project structure anyway, the projects will be converted to .dlls and .exe's.

So long as you set the Build Action to Copy, it'll wind up in your output folder directory after building. You will need to switch the path you're reading from based on whether the project is published or not.

If you're using WPF/ClickOnce, you could use

var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ApplicationDeployment.IsNetworkDeployed ? "" : "../../", "DataAccess/db.mdf");  

Upvotes: 0

Related Questions