Entity Framework, in Code First Manner, can auto-generate the scaffolding for accessing a database from an app from an app’s model class. In a previous blog I covered some issues wrt using Entity Framework Core with a Universal Windows (Platform) app with a Sqlite backend database. The entities are specified as classes in a .Net Core class project separate to the app project (within the same solution). The issue addressed in that blog was a problem referencing the class library from the UWP project. This blog revisits the same topic with an emphasis upon the steps required to implement an EF Core backed UWP app.
This article follows the steps in Getting Started with EF Core on Universal Windows Platform (UWP) with a New Database from Microsoft docs.
Its also uses my hints in Entity Framework with UWP and SQLite: Referencing the Model
Using Visual Studio 15.5.7
Using EF Version 2.1.1 in what follows.
My worked solution is on GitHub at DJsEFBlogExample
Numbered headings are as per the Getting Started with EF Core document.
Steps As per documentation
NB: Whereas when UWP app is added to the solution later it has to be unloaded to edit its .csproj file, in this first part you can directly edit the class library’s .csproj from Solution Explorer without unloading that project.
Otherwise get build error later:
Severity
Code
Description
Project
File
Line
Suppression State
Error
The TargetFramework value 'netcoreapp2.0;netstandard2.0' is not valid. To multi-target, use the 'TargetFrameworks' property instead.
DJsEFBlogExample
C:\Program Files\dotnet\sdk\2.1.4\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.TargetFrameworkInference.targets
100
Also (later) had a build error with the LOC:
optionsBuilder.UseSqlite("Data Source=blogging.db");
UseSqlite() was in error:
Needed to add Microsoft.EntityFrameworkCore.Sqlite through Nuget which added the following additional’s , amongst others:
So the added packages (dependent packages not showing):
As per documentation
On adding UWP project used .NET 4.6.1, Fall Creators Edition target platform and min as well.
If I tried to reference the class library from the UWP app I get:
Unload the UWP app and edit the ,csproj for it:
Add the <ItemGroup>,,</ItemGroup> code in bold below just before <PropertyGroup> tag in the class library .csproj file.
<ItemGroup> <ProjectReference Include="..\DJsEFBlogExample\DJsEFBlogExample.csproj"> <Project>{6F280D3E-A311-4379-B51E-A27519EB8B5D}</Project> <Name>DJsEFBlogExample</Name> </ProjectReference> </ItemGroup> <PropertyGroup Condition ..
DJsEFBlogExample is the class library name.
The project Guid, {6F280D3E-A311-4379-B51E-A27519EB8B5D}, is from the Solution file:
Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.27130.2036 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DJsEFBlogExample", "DJsEFBlogExample\DJsEFBlogExample.csproj", "{6F280D3E-A311-4379-B51E-A27519EB8B5D}" EndProject Global
Then reload the project:
The class library IS now referenced by the UWP project
NB: Had to go to Configuration Manager via Solution Explorer and set the class library to be built:
And for the UWP app to be deployed when the app runs.
Set The class library as the Startup project in Solution Explorer.
In Nuget console run add-migration command:
PM> add-migration june_25_18_1 To undo this action, use Remove-Migration. PM>
I date each migration and version number it within the day.
Can see the scaffolding added to the class library: 3 files
Before running the app,I want to examine the created db file. In App.xaml,cs in Onlaunched() add the code in blue:
protected override void OnLaunched(LaunchActivatedEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; Windows.Storage.StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; System.Diagnostics.Debug.WriteLine("Local: " +local.Path);
I then set the UWP app as the startup app and rebuilt the solution:
1>------ Rebuild All started: Project: DJsEFBlogExample, Configuration: Debug Any CPU ------ 1>DJsEFBlogExample -> C:\Users\david\source\repos\DJsEFBlogExample\DJsEFBlogExample\bin\Debug\netstandard2.0\DJsEFBlogExample.dll 1>Done building project "DJsEFBlogExample.csproj". 1>DJsEFBlogExample -> C:\Users\david\source\repos\DJsEFBlogExample\DJsEFBlogExample\bin\Debug\netcoreapp2.0\DJsEFBlogExample.dll 1>Done building project "DJsEFBlogExample.csproj". 2>------ Rebuild All started: Project: MyBlog, Configuration: Debug x86 ------ 2> MyBlog -> C:\Users\david\source\repos\DJsEFBlogExample\MyBlog\bin\x86\Debug\MyBlog.exe ========== Rebuild All: 2 succeeded, 0 failed, 0 skipped ==========
NB: Have removed some Analyser warnings from the above output. A search indicates a mismatch of DLL versions. Will look into it later.
I then ran the app and the Debug output was:
Local: C:\Users\david\AppData\Local\Packages\22f370de-21f4-4075-926f-69b13aa2f6a7_damzpawhsp48w\LocalState
No db there yet
Add the following code to the App constructor:
using (var db = new BloggingContext()) { db.Database.Migrate(); }
But got referencing issue:
So let it add the using statement. Similarly needed a using statement for Migrate():
using EFGetStarted.UWP; using Microsoft.EntityFrameworkCore;
When the app ran got the db file in the app local folder:
Opened the database with DB Browser for SQLite app:
So the db was created with the Blogs and Posts tables !
As per the documentation
The app then built and ran as expected
Records were added to the database: