我有一个解决方案(所有的.NET 4.5项目),它有一个Windows服务,一个WPF UI应用程序和一个数据层,其中我有我的EDMX模型和一个通用的存储库。WPF应用程序忽略AppDomain.CurrentDomain.SetData值
当我调试服务时,数据在数据库中结束,我想让UI在启动时使用相同的数据库文件。
所以我创建了一个目录像这样:
[solution root directory]\Data
在我把MDF和LDF在我确信我的EDMX模型使用,当我做更新,这样这个MDF文件。
对于服务,并使用相同的MDF文件中的用户界面,我确信他们的连接字符串中使用|DataDirectory|
象征,做到这一点时,他们都开始:
public MainWindow()
{
string dataDir = Utilities.Utilities.ReturnDataDirectory(System.Reflection.Assembly.GetExecutingAssembly());
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
InitializeComponent();
Loaded += MainWindow_Loaded;
}
下面的代码为ReturnDataDirectory
:
public static string ReturnDataDirectory(Assembly referingAssembly)
{
string curDir = referingAssembly.Location;
string dataDir = string.Empty;
int x;
var curDirArray = curDir.Split('\\');
//Return a directory without the 'Solution.Name\bin\Debug' part...
for (x = 0; x < (curDirArray.Count() - 4);x++)
{
dataDir += string.Format("{0}\\", curDirArray[x]);
}
return string.Format("{0}Data", dataDir);
}
这会生成MDF文件确实存在的确切目录,所以问题不在AFAIK中。
然而,当我开始的WPF UI,我得到这个错误:
An attempt to attach an auto-named database for file C:\\Dropbox\\My Docs\\MySolution\\MySolution.UI\\bin\\Debug\\MySolution.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
为什么地球上它仍然试图正确设置|DataDirectory|
照顾呢?那里没有MDF文件,这就是我目前的打算。
<add name="MySolutionEntities" connectionString="metadata=res://*/MySolution.csdl|res://*/MySolution.ssdl|res://*/MySolution.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\MySolution.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
我缺少什么:
这里在WPF UI AP的app.config中使用的连接字符串?
谢谢!
编辑:此方法适用于Windows服务项目,但不适用于WPF UI应用程序。是不是因为我太晚设置了|DataDirectory|
?我也尝试嗅探使用过程监视器的东西,但我无法获取尝试访问MDF的UI应用程序:(