简体   繁体   English

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

[英]Using a SQL Server Database File from a Class Library in C#

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

 \\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? 请?

PS I tried this, but it's not working: PS我尝试了这个,但它不起作用:

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. 连接字符串配置了无效的LocalDB服务器名称。 This may have been set in 'global.asax' by a pre-release version of MVC4. 这可能是由MVC4的预发布版本在'global.asax'中设置的。 The default connection factory is now set in web.config so the line in 'global.asax' starting with 'Database.DefaultConnectionFactory = 'should be removed. 现在,在web.config中设置了默认连接工厂,因此应删除以'Database.DefaultConnectionFactory ='开头的'global.asax'中的行。 See http://go.microsoft.com/fwlink/?LinkId=243166 for details 有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=243166

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; 我没有逃脱\\v11.0; it should have been \\\\v11.0; 应该是\\\\v11.0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM