How to install DataDirectory in the connection string to a web project?

My solution has a lib class project with a database (my DAL) and a web project that will act as a WCF service level.

I add a link to the DAL project from the web project, and when I compile, my database is copied to the Bin folder of the web project.

The Web.config file requires a connection string. If I use:

attachdbfilename=|DataDirectory|\Test.mdf 

he searches for the database in the App_Data folder.

I can easily place a copy of the database in the App_Data folder, however at this stage of development I make frequent changes to the scheme (via a copy in DAL) and I would like the project to load the version copied to the bin folder of the WCF layers.

I know that I can change the default DataDirecty as follows:

 AppDomain.CurrentDomain.SetData("DataDirectory", path); 

A) Without using a hard-coded absolute path, is there a better way to force the connection string to use the bin folder?

B) If I change the default DataDirectory parameter, how can I programmatically get an absolute link to the bin folder?

<Rant> Should Visual Studio 2010 be smart enough to implement project types, and instead of pulling the database from the referenced assembly into the bin folder, the App_Data folder was used instead? </Rant>

+4
source share
1 answer

In your web project, you should find the path by following these steps:

 string path = Server.MapPath("~/bin/Test.mdf"); 

Then you can set the path to Application_Start in the Global.ascx.cs file

 AppDomain.CurrentDomain.SetData("DataDirectory", path); 

I understand that this answers part B of your question, but not part A.

+1
source

All Articles