使配置文件中的ConnectionString的AttachDbFilename相对 [英] Make the connectionstring's AttachDbFilename relative in config file

查看:351
本文介绍了使配置文件中的ConnectionString的AttachDbFilename相对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作用在本地使用实体框架code首先生成一个MDF文件的项目。这个MDF的路径是用设置在几个配置文件在我的解决方案<&是connectionStrings GT; 像这样几个部分:

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 :

&LT;添加名称=的DataContext的connectionString =数据Source=(LocalDB)\\v11.0;AttachDbFilename=&quot;E:\\path\\to\\project\\app_data\\local.mdf&quot;;Integrated安全=真的providerName =System.Data.SqlClient的/&GT;

我使用git versionning这个项目无论是从工作和家庭,从而在工作中密度板文件路径必须是 E:\\路径\\为\\项目\\ app_data文件\\ local.mdf \\ 和家里 D:\\ otherpath \\到\\项目\\ app_data文件\\ local.mdf

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.

这是痛苦的改变,每次我comute(第一世界的问题,我知道)。

This is painful to change everytime I comute (first world problem, I know).

我已经看到了如何设置替换字符串,但这好像是用code中的配置文件之外,我不希望出现这种情况。也许有设置一个相对的一种方法| DataDirectory目录|在配置文件中的价值?

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 ?

我可以做这个路径相对于紧挨着我.sln文件一个独特的地方,只使用配置文件?

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 :

&LT;添加名称=的DataContext的connectionString =数据Source=(LocalDB)\\v11.0;AttachDbFilename=&quot;|RelativeToWorkplaceDynamicPath|\\local.mdf&quot;;Integrated安全=真的providerName =System.Data.SqlClient的/&GT;

感谢。

推荐答案

我想我想出如何使这项工作。

I think I figured out how to make this work.

我在详细解释:<一href=\"http://stackoverflow.com/questions/25185383/how-to-embed-a-database-in-a-visual-studio-solution/25211949#25211949\">How在Visual Studio解决方案中嵌入数据库?

总之,你先从替换字符串连接字符串| DataDirectory目录|。然后设置你目前的AppDomain的DataDirectory目录设置为你希望它是,您可以访问数据库之前。

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.

这篇关于使配置文件中的ConnectionString的AttachDbFilename相对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆