views:

920

answers:

2

I have the following project solution:

  • There is a ASP.NET MVC Web Application where I have a SQL database (mdf file) in the App_Data directory.

  • There is a class library where I have some migrations from migratordotnet. In my build file I have to specify where the SQL database is so it can run the migrations.

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.

+2  A: 

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.

AnthonyWJones
Nope. I'm quite new at this SQL Express thing.
Spoike
+2  A: 

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.

Kevin Hobbs