Search code examples
wpfentity-framework-5design-time-data

No design time data with entity framework 5 in Visual Studio


Hi I build a simple test solution in VS 2012 with two projects.

One is a Class library and the other a WPF application. Both use .NET 4.5.

In the class library I add an EF element (DataFirst) which maps a simple table.

Next I reference this project in my WPF project. Add EF from Nuget and using a very simple MVVM like pattern I add a class which looks like this (please note - this is not production code - it's just to reproduce the problem).

public class Class1 {
public static Helper TheHelper { get; set; }
public Class1() {
    TheHelper = new Helper();
}

} public class 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

I also extended the context class like this:

public partial class FCdataEntities : DbContext {
    public FCdataEntities(string strCon) : base(strCon) {
    }
}

So I can pass the connection string which I copy from app.config.

In my main window I do a simple binding like this:

        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?


Solution

  • This particular error is a known issue in Entity Framework 5 with the Visual Studio & Expression designers and has been fixed in the Entity Framework 6 source code. However there are other design time errors which I haven't been able to resolve which prevent Entity Framework code from running in the designer.