Reputation: 940
I am working on a project using an mdf file generated locally using Entity Framework Code First. The path to this mdf is set in several config files in my solution using <connectionStrings>
sections like so :
<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, thus at work the mdf filepath has to be E:\path\to\project\app_data\local.mdf\
and at home D:\otherpath\to\project\app_data\local.mdf
.
This is painful to change everytime I comute (first world problem, I know).
I have seen how to set a substitution string but this seems to be using code outside the config file and I don't want that. Maybe there is a way to set a relative |DataDirectory| value inside the config file ?
Can I make this path relative to a unique place next to my .sln file, using only those config files ?
This would ideally be something like that :
<add name="DataContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename="|RelativeToWorkplaceDynamicPath|\local.mdf";Integrated Security=True" providerName="System.Data.SqlClient" />
Thanks.
Upvotes: 2
Views: 5640
Reputation: 11710
I think I figured out how to make this work.
I explain in detail at: How to embed a database in a visual studio solution?
In short, you start your connection string with the substitution string "|DataDirectory|". And then you set your current AppDomain's "DataDirectory" setting to where you want it to be, before you access the database.
It's not perfectly clean, but it's workable.
Upvotes: 4