efcore: Cannot Reference ViewModel with EFCore in Contructor from UWP XAML

Create the EFGetStarted.UWP app with all prerequisites in accordance with UWP - New Database

Add a class MainPageViewModel.cs

    namespace EFGetStarted.UWP
    {
        public class MainPageViewModel
        {
            public MainPageViewModel()
            {
                using (var db = new BloggingContext())
                {
                }
            }
        }
    }

Add the following using directive to MainPage.xaml xmlns:ViewModels="using:EFGetStarted.UWP" and the following

    <Page.DataContext>
        <ViewModels:MainPageViewModel x:Name="MainPageViewModel" />
    </Page.DataContext>

In VS2015 Update 3there is a build error

Could not load file or assembly ‘Microsoft.EntityFrameworkCore, Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60’ or one of its dependencies. The system cannot find the file specified. EFGetStarted.UWP

project.json

{
     "dependencies": {
     "Microsoft.EntityFrameworkCore.Sqlite": "1.1.0",
     "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
     "Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2"
 },

```    "frameworks": {
     "uap10.0": {}
 },
 "runtimes": {
     "win10-arm": {},
     "win10-arm-aot": {},
     "win10-x86": {},
     "win10-x86-aot": {},
     "win10-x64": {},
     "win10-x64-aot": {}
     }
 }

App.config

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.IO.FileSystem.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="4.0.0.0" newVersion="4.0.1.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Overlapped" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="4.0.0.0" newVersion="4.0.1.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="4.1.0.0" newVersion="4.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Comment out the using statement and the code builds. Or comment out

<ViewModels:MainPageViewModel x:Name="MainPageViewModel" />

and the code builds.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 17 (8 by maintainers)

Most upvoted comments

Just to add up to that. I get an error only on x86 configuration, which is required for Design Data to work.

However I’ve just found a fancy solution. You just need to put your context initialization in a different function. For instance:

public class MainPageViewModel
{
        private DBContext context;

        public MainViewModel()
        {
            if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
            {
                // design-time data
            }
            else
            {
                InitializeContext();
            }
        }

        private void InitializeContext() => context = new DBContext();
}

And this is strange, but it works.