在Visual Studio中没有实体框架5的设计时数据

时间:2013-02-11 09:57:40

标签: wpf entity-framework-5 design-time-data

您好我在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();
}

} 公共级助手{     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>

解决方案在运行时运行良好。 但是在VS Designer中,我在我的“虚拟对象”中显示了一个异常,我在该属性的catch块中创建了该异常。

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)

我需要的是设计时的数据 - 而不是“虚拟数据” - 而是我想从数据库中获取它 - 避免使用EF(并使用linq2sql作为示例)就像魅力一样。

我是否对连接字符串(或左右)犯了错误 - 或者EF 5.0中是否存在问题?

1 个答案:

答案 0 :(得分:1)

此特定错误是Entity Framework 5中使用Visual Studio&amp;表达式设计者已经在Entity Framework 6中修复了源代码。但是,我还无法解决其他设计时错误,这些错误会阻止Entity Framework代码在设计器中运行。