简体   繁体   中英

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

So I have a basic 3-tier application:

  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

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?

PS 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

Thanks to wdosanjos for pointing out my stupid mistake.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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