In this article I discuss two ways to the refer a UWP app to an Entity Framework Model Class Library, which appears to not be possible through the normal method of adding references between projects in Visual Studio.


Entity Framework enables developers to create a model of the target system as a library of (C#) classes and can then automatically generate the coding infrastructure to implement the model’s database as well as to access it at runtime, i.e. implement the CRUD code. It eliminates the classical methods for accessing a database where you have to manually implement database connectivity and CRUD code. In reverse, EF can also generate the model class library and infrastructure from an existing database.

There are actually  several ways to setup EF:

Entity Framework Development Approaches

The two methods discussed above are both Code First.:

Code First

Whether you have an existing database or not, you can use the Entity Framework without using the designer or an .edmx file. If you don't have a database, you can code your own classes and properties that correspond to tables and columns. If you do have a database, Entity Framework tools can generate the classes and properties that correspond to existing tables and columns. The mapping between the store schema and the conceptual model represented by your code is handled by convention and by a special mapping API. If you let Code First create the database, you can use Code First Migrations to automate the process of deploying the database to production. Migrations can also automate the deployment of database schema changes to production when your data model changes.


There is a pivotal article on Microsoft Docs that covers building a UWP app that uses Entity Framework Core to access a SQLite database that reflects a model class library.

Getting Started with EF Core on Universal Windows Platform (UWP) with a New Database

The structure of the project is .NET Standard Class Library in which there are Blogs and Posts. A Blog has one or more Posts. The BloggingContext is what is written to and from the database. For this activity a local SQLite database is used.


The UWP app displays the list of blogs as retrieved from the database. Blogs can be added. This app doesn’t implement deletion and update of blogs. Also, it doesn’t implement any Post functionality.


The Referencing Issue

There has been some discussion of issues with respect to getting this tutorial to work. I have contributed some comments. One recent query has been how to reference the model library from the UWP app. I having been looking into this issue.

Recreating the project, starting from scratch as per the article, if I just attempt to add the model reference directly to the app (Right-click on the app References in Solution Explorer and choose Add Reference) I get:


Without this reference in the app project, the classes in the model library to which it refers are not available. Also the required using statement:

using Microsoft.EntityFrameworkCore;

is unreferenced, as well as references in the app to the model fail.

I did get the GitHub version of the code working so I drilled into the app solution file and the app project. Using a file comparison app I compared both files between the GitHub version and my newly created one. I was able to add the required reference to the model project by the app in the app’s project file. I needed to get the model project’s Guid from the app solution file. It did give me a warning on building the app that it couldn’t find the model but the app did compile and run with the manually inserted reference.


So the question is how to add the model reference in a more formal way?


How to add the reference (Take One)

The code as per the GitHub project (modified slightly) is:


App Project file:

    <ProjectReference Include="..\EFBlogs\EFBlogs.csproj">

The EFBlogs (same in three places) is the name of my model project.

The Guid was located in the Solution file near the top:


Project Solution File:

Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFBlogs", "EFBlogs\EFBlogs.csproj", "{01342E99-8926-4D3E-96CC-03A69B86739C}" 

So I modified the new app accordingly:


App Project file (new app):


    <ProjectReference Include="..\EFBlogs\EFBlogs.csproj">


In Project Solution File(new app):

Near the top:

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFBlogs", "EFBlogs\EFBlogs.csproj", "{FCC194ED-A2F9-44E0-9772-75F1C1D51299}"


With this reference inserted the app works.Smile


Hint: To edit a project or solution file for a project loaded in Visual Studio, you may need to Unload the project first (Right-Click on it in Solution Explorer and choose Unload). You can edit it as XML, save when done then Reload it. In some circumstances you can directly edit a project file without unloading it, but will need to reload the project.


Take Two (For Adding the Reference)

Another approach I used was to (and doing a build after each step to check):

1. Create the model library as blank Class Library (.NET Standard), removing the Class1 code and the using System reference.

2. Create the blank UWP app and add a reference to the model library as usual.

3. Embellish the model library with its code and Nuget references.

4. Action the Migration scaffolding

5. Embellish the UWP app code with its UI code and the model library references.


Migration Issues

(1) If you get the message as below for Add-Migration

PM> Add-Migration MyFirstMigration

Startup project 'EFBlogs3' targets framework '.NETStandard'. There is no runtime associated with this framework, and projects targeting it cannot be executed directly. To use the Entity Framework Core Package Manager Console Tools with this project, add an executable project targeting .NET Framework or .NET Core that references this project, and set it as the startup project; or, update this project to cross-target .NET Framework or .NET Core


…it means you haven’t done the change in the <Target Frameworks> tag in the <Property Group> branch of the model class library project file:





(2)  Also I found that you might not get any outcome to the Add-Migration command.
This was because you also need to add a second item in the model project file to the
<Property Group> branch:



(3) As “smoke and mirrors” as it sounds, when performing the migration, make sure that besides being the Default project in the PM Console, make sure it’s the Startup project. (And when you run the app make the app the Startup.)

All being OK the outcome is:

PM> Add-Migration MyFirstMigration
To undo this action, use Remove-Migration.

And in Solution Explorer you can see the Migrations added (as highlighted):


As indicated you can undo this scaffolding by running the Remove-Migration command. Also if your model changes you can rerun the Add-Migration command to regenerate the scaffolding, and the database, but your existing data will be lost.



Hope this helps Smile