嗨,我在 VS 2012 中用两个项目构建了一个简单的测试解决方案。
一个是类库,另一个是 WPF 应用程序。两者都使用 .NET 4.5。
在类库中,我添加了一个映射简单表的 EF 元素 (DataFirst)。
接下来我在我的 WPF 项目中引用这个项目。从 Nuget 添加 EF 并使用非常简单的 MVVM 模式我添加了一个看起来像这样的类(请注意 - 这不是生产代码 - 它只是为了重现问题)。
public class Class1 {
public static Helper TheHelper { get; set; }
public Class1() {
TheHelper = new Helper();
}
} 公共类 Helper { public Helper() { Nam = "aaa";
}
string connectionString = "metadata=res://*/Mod.csdl|res://*/Mod.ssdl|res://*/Mod.msl;provider=System.Data.SqlClient;provider connection string=\";data source=.\\sqlx8r2;initial catalog=FCdata;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework\"";
public string Nam { get; set; }
#region PCs
private List<PC> m_lPCs;
public List<PC> PCs {
get {
if(m_lPCs == null) {
try {
using(FCdataEntities dE = new FCdataEntities(connectionString)) {
m_lPCs = dE.PCs.ToList();
}
}
catch(Exception eX) {
m_lPCs = new List<PC>();
m_lPCs.Add(new PC() { Description = eX.Message });
}
}
return m_lPCs;
}
set {
if(m_lPCs != value) {
m_lPCs = value;
//RaisePropertyChanged(() => PCs);
}
}
}
#endregion
我还像这样扩展了上下文类:
public partial class FCdataEntities : DbContext {
public FCdataEntities(string strCon) : base(strCon) {
}
}
所以我可以传递从 app.config 复制的连接字符串。
在我的主窗口中,我做了一个简单的绑定,如下所示:
xmlns:local="clr-namespace:EFTest"
Title="MainWindow" Height="350" Width="1525">
<Window.Resources>
<local:Class1 x:Key="dG" />
</Window.Resources>
<Grid DataContext="{Binding Path=TheHelper, Source={StaticResource dG} }">
<Grid.RowDefinitions>
<RowDefinition Height="17*"/>
<RowDefinition Height="143*"/>
</Grid.RowDefinitions>
<TextBox Text="{Binding Nam}" />
<ListBox ItemsSource="{Binding PCs}" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Description}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
The solution works fine at runtime. But in the VS Designer I get an exception shown in my "dummy obect" which I create in the catch block of the property.
Could not load file or assembly 'Windows, Version=255.255.255.255, Culture=neutral, ContentType=WindowsRuntime' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
What I need is data at design time - and no "dummy data" - instead I want to get it from the DB - avoiding the use of EF (and use linq2sql for an example) works like a charm.
Did I make a mistake with the connection string (or so) - or is there simply a problem in EF 5.0?