如何正确设置Web项目连接字符串中的DataDirectory? [英] How do I properly set the DataDirectory in a web project connection string?

查看:143
本文介绍了如何正确设置Web项目连接字符串中的DataDirectory?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案有一个数据库(我DAL)和Web项目作为一个WCF服务层的类的lib项目。

My solution has a class lib project with a database (my DAL), and a web project to act as a WCF service layer.

我添加引用从Web项目的DAL项目,当我编译,我的数据库复制到Web项目的Bin文件夹。

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.

在Web.config文件需要连接字符串。如果我使用:

The Web.config file needs the connection string. If I use:

attachdbfilename=|DataDirectory|\Test.mdf

它看起来为DB在App_Data文件夹中。

it looks for the DB in the App_Data folder.

我可以很容易地把数据库的副本App_Data文件夹,但是,在这个阶段的发展,我做的模式(通过副本频繁变动该DAL),并希望项目以皮卡复制到WCF层bin文件夹中的版本

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.

我知道我可以改变这个默认DataDirecty:

I know I can alter the default DataDirecty with this:

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



A)如果不使用硬编码的绝对路径,有没有更好的方式来获取连接字符串使用bin文件夹?

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

b)如果我做改变默认DataDirectory目录设置,我怎么编程获得绝对参考bin文件夹?

B) If I do alter the default DataDirectory setting, how do I programmatically obtain the absolute reference to the bin folder?

<夸大其词> 不应该的Visual Studio 2010已经足够智能,实现了项目类型和,而不是从拉DB引用的程序集到bin文件夹,用来App_Data文件夹呢? < /夸大其词>

<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>

推荐答案

在您的Web项目,你应该能够找到通过执行路径:

In your web project you should be able to find the path by doing:

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

您可以再设置的Application_Start 路径在Global.ascx.cs

You can then set the path in Application_Start in your Global.ascx.cs

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



我知道这回答你的问题的一部分,B,而不是A部分。

这篇关于如何正确设置Web项目连接字符串中的DataDirectory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆