在C#中使用类库中的SQL Server数据库文件

时间:2023-01-15 15:46:13

So I have a basic 3-tier application:

所以我有一个基本的3层应用程序:

  1. Data Access

  2. Business Logic

  3. Presentation

My Data Access layer is a class library, and I'm using Entity Framework and a SQL Server Database File

我的数据访问层是一个类库,我正在使用实体框架和SQL Server数据库文件

Say my solution is structured as such

说我的解决方案是这样构建的

MySolution

\MySolution.Data

    \MySolution.Data\App_Data\MySolutionDB.mdf

\MySolution.BusinessLogic

\Presentation

The problem I am trying to solve is that I need to get a folder relative path to the database for the connection string because this project will not always be deployed in into the same folder structure, therefore it is imperative that the connection string is created dynamically based on the relative path.

我试图解决的问题是我需要为连接字符串获取数据库的文件夹相对路径,因为此项目不会始终部署到同一文件夹结构中,因此必须动态创建连接字符串基于相对路径。

Could someone offer some assistance on this. please?

有人可以提供一些帮助。请?

P.S. I tried this, but it's not working:

附:我尝试过这个,但它不起作用:

string.Format("Data Source=(LocalDB)\v11.0;AttachDbFilename={0}\\MySolutionDB.mdf;Integrated Security=True", AppDomain.CurrentDomain.GetData("DataDirectory"))

EDIT: Getting this error message, which may be unrelated to the issue...

编辑:获取此错误消息,这可能与该问题无关...

Connection to the database failed. The connection string is configured with an invalid LocalDB server name. This may have been set in 'global.asax' by a pre-release version of MVC4. The default connection factory is now set in web.config so the line in 'global.asax' starting with 'Database.DefaultConnectionFactory = 'should be removed. See http://go.microsoft.com/fwlink/?LinkId=243166 for details

与数据库的连接失败。连接字符串配置了无效的LocalDB服务器名称。这可能是由MVC4的预发布版本在'global.asax'中设置的。现在,在web.config中设置了默认连接工厂,因此应删除以'Database.DefaultConnectionFactory ='开头的'global.asax'中的行。有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=243166

1 个解决方案

#1


3  

Thanks to wdosanjos for pointing out my stupid mistake.

感谢wdosanjos指出我的愚蠢错误。

string.Format("Data Source=(LocalDB)\v11.0;AttachDbFilename={0}\\MySolutionDB.mdf;Integrated Security=True", AppDomain.CurrentDomain.GetData("DataDirectory"))

I did not escape \v11.0; it should have been \\v11.0;

我没有逃脱\ v11.0;应该是\\ v11.0;

#1


3  

Thanks to wdosanjos for pointing out my stupid mistake.

感谢wdosanjos指出我的愚蠢错误。

string.Format("Data Source=(LocalDB)\v11.0;AttachDbFilename={0}\\MySolutionDB.mdf;Integrated Security=True", AppDomain.CurrentDomain.GetData("DataDirectory"))

I did not escape \v11.0; it should have been \\v11.0;

我没有逃脱\ v11.0;应该是\\ v11.0;