1

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?

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

7
  • try Environment.CurrentDirectory() + @"\App_Data\" + MySolutionDB.mdf Commented Nov 8, 2013 at 20:40
  • What happens? Do you get an error message? Commented Nov 8, 2013 at 20:40
  • 1
    My suggestion is to keep connection information out of the class library. Instead, set up a connection string in the app that uses it. Commented Nov 8, 2013 at 20:44
  • @wdosanjos, yes getting an error message...see update Commented Nov 8, 2013 at 20:48
  • I think your are missing a \ in (LocalDB)\v11.00. I think it should be (LocalDB)\\v11.00 Commented Nov 8, 2013 at 20:50

1 Answer 1

3

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;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.