开发者

How do I properly set the DataDirectory in a web project connection string?

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 t开发者_开发百科he 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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜