【问题标题】:No design time data with entity framework 5 in Visual StudioVisual Studio 中的实体框架 5 没有设计时数据
【发布时间】:2013-01-26 09:54:11
【问题描述】:

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

} 公共类助手{ 公共助手(){ 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 中存在简单的问题?

【问题讨论】:

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


    【解决方案1】:

    此特定错误是 Entity Framework 5 中 Visual Studio 和 Expression 设计器的一个已知问题,已在 Entity Framework 6 源代码中修复。但是,还有其他一些我无法解决的设计时错误会阻止实体框架代码在设计器中运行。

    【讨论】:

    • 这些错误是否已在当前版本中修复 - 或者 EF 代码在设计器中仍然存在问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多