Reputation: 3151
My solution has a class lib project with a database (my DAL), and a web project to act as a WCF service layer.
I add a reference 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 needs the connection string. If I use:
attachdbfilename=|DataDirectory|\Test.mdf
it looks for the DB in the App_Data folder.
I can easily place a copy of the DB in the App_Data folder, however, at this stage in development I'm making frequent changes to the schema (via the copy in the DAL), and would like the project to pickup the version copied to the WCF layers bin folder.
I know I can alter the default DataDirecty with this:
AppDomain.CurrentDomain.SetData("DataDirectory", path);
A) Without using a hard-coded absolute path, is there a better way to get the connection string to use the bin folder?
B) If I do alter the default DataDirectory setting, how do I programmatically obtain the absolute reference to the bin folder?
<Rant>
Shouldn't Visual Studio 2010 have been smart enough to realize the project types and instead of pulling the DB from the referenced assembly into the bin folder, used the App_Data folder instead? </Rant>
Upvotes: 4
Views: 4536
Reputation: 598
In your web project you should be able to find the path by doing:
string path = Server.MapPath("~/bin/Test.mdf");
You can then set the path in Application_Start
in your Global.ascx.cs
AppDomain.CurrentDomain.SetData("DataDirectory", path);
I realise this answers part B of your question but not part A.
Upvotes: 1