Getting Started with EntityFramework on Universal Windows Platform (UWP)


Click here to change the theme.

Originally the article Getting Started on UWP - New Database - EF Core was about the Entity Framework, not the "core" version. Microsoft appears to be pushing the "core" version of everything. They seem to be moving too fast since they themselves are not yet able to fully support UWP; that article (currently) says:

Warning

Due to limitations in the way .NET Core tools interact with UWP projects the model needs to be placed in a non-UWP project to be able to run migrations commands in the Package Manager Console.

So you might consider it to not be ready for prime-time. That warning does not apply to this article. This article can be used to avoid the bleeding edge.

This article began as notes for myself. I have tried to trim out stuff that most of us already know. I hope I did not omit too much. This article describes use of Entity Framework Code-First and Migrations. Migrations will create a new database if one does not exist and will support changes to the production database in the future when that is necessary. Something I discovered however is that migrations do not suppport many things for UWP that is supported by other types of applications.

This article omits the UI. I hope you can provide that yourself. The Microsoft article provides a sample UI.

Requirements

NuGet version 3.5 or greater (look in "Help" | "About Microsoft Visual Studio" for "NuGet Package Manager"); if NuGet is below version 3.5 then go to the NuGet download page.

The NuGet package Microsoft.NETCore.UniversalWindowsPlatform must be version 5.2.2 or higher.

Creating a project using EF

Create a blank Universal Windows application.

Then install the following:

  • Microsoft.EntityFrameworkCore.Sqlite
  • Microsoft.EntityFrameworkCore.Tools (must be version 1.1.1)

Database Processing

Add a context (such as BloggingContext) class derived from DbContext (add a using for Microsoft.EntityFrameworkCore). Tables consist of public properties. The context class has one "DbSet<table> tables" for each table and an "OnConfiguring" member as in the following:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
        optionsBuilder.UseSqlite("Data Source=blogging.db");
}

To create a database, in the NuGet Package Manager Console run:

Add-Migration MyFirstMigration

When changes are made to the model in code, use the Add-Migration command to apply the changes to the database. (That is what the article said but I amnot sure of the details.)

Then add code to create the local database when the application starts the first time it runs. In the code for App.xaml add a using (for Microsoft.EntityFrameworkCore?) then add the following:

public App()
{
    this.InitializeComponent();
    this.Suspending += OnSuspending;
    using (var db = new BloggingContext())
    {
        db.Database.Migrate();
    }
}

User Interface

If you have not created a UI yet then this is a good time to do that. You can create a Page_Loaded event for the XAML generated for the project. In the XAML code-behiind, use something as in the following for the Page_Loaded event:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    using (var db = new BloggingContext())
    {
        Blogs.ItemsSource = db.Blogs.ToList();
    }
}

And something such as the following to add a record:

using (var db = new BloggingContext())
{
var blog = new Blog { Url = NewBlogUrl.Text };
db.Blogs.Add(blog);
db.SaveChanges();
Blogs.ItemsSource = db.Blogs.ToList();
}

The updates of the data and removal of records are similar.

References

SQLite

Intro to the Universal Windows Platform - UWP app developer | Microsoft Docs

UWP - New Database | Microsoft Docs

Microsoft.Data.Sqlite: SQLite implementations of the System.Data.Common interfaces

Using SQLite databases in UWP apps - Building Apps for Windows