question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot Reference ViewModel with EFCore in Contructor from UWP XAML

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Andrew-Bycommented, Jan 18, 2017

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.

0reactions
ryanwintercommented, Mar 5, 2017

@Andrew-By 's workaround is good for me too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to reference a design-time ViewModel in Xaml
In short, I've got a design-time model that inherits from my real view model and sets some properties in the constructor.
Read more >
uwp with c# and mvvm inversion of control (dependency ...
7.Resolve the MainViewModel , and inject the dependencies by using ActivatorUtilities function to inject parameters into the constructor.
Read more >
Building Modern Desktop Apps with .NET and C - WinUI 3.0
WinUI 3.0 is an evolution of the UWP XAML framework. Then there are also cross-platform frameworks like .NET MAUI and Uno Platform.
Read more >
Error in designer when used in constructor · Issue #89
This works at runtime, this is an issue of during design time in the UWP-XAML designer. This occurs when the constructor of the...
Read more >
Learn How to Use Dependency Injection in .NET MAUI
This blog explains the dependency injection technique and how to easily use it in your .NET MAUI applications with code examples.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found