Make connectionString AttachDbFilename relative in config file
I am working on a project using an mdf file generated locally using First Entity Framework Code. The path to this mdf is set in multiple config files in my solution using sections <connectionStrings>
like:
<add name="DataContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename="E:\path\to\project\app_data\local.mdf";Integrated Security=True" providerName="System.Data.SqlClient" />
I use git versionning on this project both from work and at home, so when working, the path to the mdf file should be E:\path\to\project\app_data\local.mdf\
at home as well D:\otherpath\to\project\app_data\local.mdf
.
It hurts to change every time I am a comet (world's first problem, I know).
I've seen how to set the replacement string, but it looks like using code outside the config file and I don't want that. Perhaps there is a way to set relative | DataDirectory | value inside config file?
Is it possible to make this path relative to a unique location next to my .sln file using only these config files?
Ideally, it would be something like this:
<add name="DataContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename="|RelativeToWorkplaceDynamicPath|\local.mdf";Integrated Security=True" providerName="System.Data.SqlClient" />
Thank.
source to share
I think I figured out how to make this work.
I explain in detail: How to embed a database in a visual studio solution?
In short, you start your connection string with the "| DataDirectory |" substitution string. And then you set the current "DataDirectory" AppDomain setting to where you want it before you access the database.
It's not entirely clean, but it is workable.
source to share