Spoike
Spoike

Reputation: 121792

How do I refer to APP_Data from another project

I have the following project solution:

For the moment on the migration project I'm using a hardcoded path to access the SQL database in the connection string. It looks sort of like this:

connectionString="Data Source=.\SQLExpress; Integrated Security=true; AttachDbFilename=C:\MySolution\MyMVCProject\App_Data\MyDatabase.mdf"

This is not how I want to do. I also cannot use relative paths (such as ..\MyMVCPProject\AppData\MyDatabase.mdf because the SQL classes used in migratordotnet won't be able to translate it right) But I want to use some kind of a substitute path to it, sort of what you'll find with |DataDirectory| in the Web.Config in the web project like this:

connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True;User Instance=True"

How do I do achieve this? I can't use that connection string in the migration project. Because the |DataDirectory| will go to .NET Framework installation path and look for the database file there.

Upvotes: 2

Views: 2977

Answers (2)

Kevin Hobbs
Kevin Hobbs

Reputation: 104

You want something like this (VB .NET code):

connectionString="Data Source=.\SQLExpress; Integrated Security=true; AttachDbFilename=" & Server.MapPath("~\App_Data\MyDatabase.mdf")

Server.MapPath() will turn your relative path into a fully qualified path at runtime.

Upvotes: 2

AnthonyWJones
AnthonyWJones

Reputation: 189485

Have you considered just attaching the database permanently in SQL Express? That way you can use identical connection strings in both projects and neither will be tied to a file system path.

Upvotes: 2

Related Questions