Pregunta

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

¿Fue útil?

Solución

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;

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top