Skip to content

Commit

Permalink
Update the UWP page to 2.x
Browse files Browse the repository at this point in the history
Fixes #446
Fixes #467
  • Loading branch information
divega authored and AndriySvyryd committed Oct 19, 2017
1 parent 8f90598 commit 0db6754
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 202 deletions.
183 changes: 42 additions & 141 deletions entity-framework/core/get-started/uwp/getting-started.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
---
title: Getting Started on UWP - New Database - EF Core
author: rowanmiller
ms.author: divega
Expand All @@ -13,49 +13,42 @@ uid: core/get-started/uwp/getting-started

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

> [!NOTE]
> Temporarily this tutorial uses EF Core 1.1. UWP has not been updated yet to support .NET Standard 2.0 which is required for compatibility with EF Core 2.0. Once it is, we will update the tutorial to use the new version.
-> [!NOTE]
-> This tutorial uses EF Core 2.0.1. EF Core 2.0.0 lacks some crucial bugfixes required for a good UWP experience.

In this walkthrough, you will build a Universal Windows Platform (UWP) application that performs basic data access against a local SQLite database using Entity Framework.

> [!WARNING]
> **Avoid using anonymous types in LINQ queries on UWP**. Deploying a UWP application to the app store requires your application to be compiled with .NET Native. Queries with anonymous types have poor performance on .NET Native or may crash the application.
> [!IMPORTANT]
> **Consider avoiding anonymous types in LINQ queries on UWP**. Deploying a UWP application to the app store requires your application to be compiled with .NET Native. Queries with anonymous types have worse performance on .NET Native.
> [!TIP]
> [!TIP]
> You can view this article's [sample](https://github.com/aspnet/EntityFramework.Docs/tree/master/samples/core/GetStarted/UWP/UWP.SQLite) on GitHub.
## Prerequisites

The following items are required to complete this walkthrough:
* Windows 10

* [Visual Studio 2017](https://www.visualstudio.com/downloads/)
* [Windows 10 Fall Creators Update](https://support.microsoft.com/en-us/help/4027667/windows-update-windows-10) (10.0.16299.0)

* [.NET Core 2.0.0 SDK](https://www.microsoft.com/net/core) or later.

* [Visual Studio 2017](https://www.visualstudio.com/downloads/) version 15.4 or later with the **Universal Windows Platform Development** workload.

* The latest version of [Windows 10 Developer Tools](https://dev.windows.com/downloads)
## Create a new model project

## Create a new project
> [!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
* Open Visual Studio

* File > New > Project...

* From the left menu select Templates > Visual C# > Windows Universal
* From the left menu select Templates > Visual C#

* Select the **Blank App (Universal Windows)** project template
* Select the **Class Library (.NET Standard)** project template

* Give the project a name and click **OK**

## Upgrade Microsoft.NETCore.UniversalWindowsPlatform

Depending on your version of Visual Studio, the template may have generated your project with an old version of .NET Core for UWP. EF Core requires `Microsoft.NETCore.UniversalWindowsPlatform` version **5.2.2** or greater.

* Tools > NuGet Package Manager > Package Manager Console

* Run `Update-Package Microsoft.NETCore.UniversalWindowsPlatform –Version 5.2.2`

> [!TIP]
> If you are using Visual Studio 2017, you can upgrade to the latest version of `Microsoft.NETCore.UniversalWindowsPlatform` and do not need to explicitly target `5.2.2`.
## Install Entity Framework

To use EF Core, install the package for the database provider(s) you want to target. This walkthrough uses SQLite. For a list of available providers see [Database Providers](../../providers/index.md).
Expand All @@ -68,6 +61,8 @@ Later in this walkthrough we will also be using some Entity Framework Tools to m

* Run `Install-Package Microsoft.EntityFrameworkCore.Tools`

* Edit the .csproj file and replace `<TargetFramework>netstandard2.0</TargetFramework>` with `<TargetFrameworks>netcoreapp2.0;netstandard2.0</TargetFrameworks>`

## Create your model

Now it's time to define a context and entity classes that make up your model.
Expand All @@ -78,50 +73,30 @@ Now it's time to define a context and entity classes that make up your model.

* Replace the contents of the file with the following code

<!-- [!code-csharp[Main](samples/core/GetStarted/UWP/UWP.SQLite/Model.cs)] -->
``` csharp
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace EFGetStarted.UWP
{
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }

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

public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }

public List<Post> Posts { get; set; }
}

public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }

public int BlogId { get; set; }
public Blog Blog { get; set; }
}
}
```
[!code-csharp[Main](../../../../samples/core/GetStarted/UWP/UWP.Model/Model.cs)]

## Create a new UWP project

* Open Visual Studio

* File > New > Project...

* From the left menu select Templates > Visual C# > Windows Universal

* Select the **Blank App (Universal Windows)** project template

* Give the project a name and click **OK**

* Set the target and minimum versions to at least `Windows 10 Fall Creators Update (10.0; build 16299.0)`

## Create your database

Now that you have a model, you can use migrations to create a database for you.

* Tools –> NuGet Package Manager –> Package Manager Console

* Select the model project as the Default project and set it as the startup project

* Run `Add-Migration MyFirstMigration` to scaffold a migration to create the initial set of tables for your model.

Since we want the database to be created on the device that the app runs on, we will add some code to apply any pending migrations to the local database on application startup. The first time that the app runs, this will take care of creating the local database for us.
Expand All @@ -130,31 +105,9 @@ Since we want the database to be created on the device that the app runs on, we

* Add the highlighted using to the start of the file

<!-- [!code-csharp[Main](samples/core/GetStarted/UWP/UWP.SQLite/App.xaml.cs?highlight=1)] -->
``` csharp
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
```

* Add the highlighted code to apply any pending migrations

<!-- [!code-csharp[Main](samples/core/GetStarted/UWP/UWP.SQLite/App.xaml.cs?highlight=6,7,8,9)] -->
``` csharp
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;

using (var db = new BloggingContext())
{
db.Database.Migrate();
}
}
```
[!code-csharp[Main](../../../../samples/core/GetStarted/UWP/UWP.SQLite/App.xaml.cs?highlight=1,25-28)]

> [!TIP]
> If you make future changes to your model, you can use the `Add-Migration` command to scaffold a new migration to apply the corresponding changes to the database. Any pending migrations will be applied to the local database on each device when the application starts.
Expand All @@ -169,70 +122,15 @@ You can now use your model to perform data access.

* Add the page load handler and UI content highlighted below

<!-- [!code-csharp[Main](samples/core/GetStarted/UWP/UWP.SQLite/MainPage.xaml?highlight=9,12,13,14,15,16,17,18,19,20,21,22)] -->
``` xaml
<Page
x:Class="EFGetStarted.UWP.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:EFGetStarted.UWP"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Loaded="Page_Loaded">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<TextBox Name="NewBlogUrl"></TextBox>
<Button Click="Add_Click">Add</Button>
<ListView Name="Blogs">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Url}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</Grid>
</Page>
```
[!code-xml[Main](../../../../samples/core/GetStarted/UWP/UWP.SQLite/MainPage.xaml?highlight=9,11-23)]

Now we'll add code to wire up the UI with the database

* Right-click **MainPage.xaml** in **Solution Explorer** and select **View Code**

* Add the highlighted code from the following listing

<!-- [!code-csharp[Main](samples/core/GetStarted/UWP/UWP.SQLite/MainPage.xaml.cs?highlight=8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26)] -->
``` csharp
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}

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

private void Add_Click(object sender, RoutedEventArgs e)
{
using (var db = new BloggingContext())
{
var blog = new Blog { Url = NewBlogUrl.Text };
db.Blogs.Add(blog);
db.SaveChanges();

Blogs.ItemsSource = db.Blogs.ToList();
}
}
}
```
[!code-csharp[Main](../../../../samples/core/GetStarted/UWP/UWP.SQLite/MainPage.xaml.cs?highlight=30-48)]

You can now run the application to see it in action.

Expand All @@ -248,6 +146,9 @@ You can now run the application to see it in action.

## Next steps

> [!TIP]
> `SaveChanges()` performance can be improved by implementing [`INotifyPropertyChanged`](https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx), [`INotifyPropertyChanging`](https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanging.aspx), [`INotifyCollectionChanged`](https://msdn.microsoft.com/en-us/library/system.collections.specialized.inotifycollectionchanged.aspx) in your entity types and using `ChangeTrackingStrategy.ChangingAndChangedNotifications`.
Tada! You now have a simple UWP app running Entity Framework.

Check out the numerous articles in this documentation to learn more about Entity Framework's features.
Check out other articles in this documentation to learn more about Entity Framework's features.
3 changes: 0 additions & 3 deletions entity-framework/core/get-started/uwp/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,3 @@ uid: core/get-started/uwp/index
These 101 tutorials require no previous knowledge of Entity Framework Core or Visual Studio. They will take you step-by-step through creating a simple Universal Window Platform (UWP) application that queries and saves data from a database.

You can find the UWP documentation at [developer.microsoft.com/windows/apps/develop](https://developer.microsoft.com/windows/apps/develop).

> [!NOTE]
> These tutorials and the accompanying samples have been updated to use EF Core 2.0 (with the exception of the UWP tutorial, that still uses EF Core 1.1). However, in the majority of cases it should be possible to create applications that use previous releases, with minimal modification to the instructions.
2 changes: 0 additions & 2 deletions samples/core/DynamicModel/DynamicContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace EFModeling.Samples.DynamicModel
{

#region Class

public class DynamicContext : DbContext
{
public bool? IgnoreIntProperty { get; set; }
Expand All @@ -32,6 +31,5 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}
}
}

#endregion
}
2 changes: 0 additions & 2 deletions samples/core/DynamicModel/DynamicModelCacheKeyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace EFModeling.Samples.DynamicModel
{

#region Class

public class DynamicModelCacheKeyFactory : IModelCacheKeyFactory
{
public object Create(DbContext context)
Expand All @@ -17,6 +16,5 @@ public object Create(DbContext context)
return context.GetType();
}
}

#endregion
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;

namespace EFGetStarted.UWP.Migrations
{
Expand All @@ -13,7 +13,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
columns: table => new
{
BlogId = table.Column<int>(nullable: false)
.Annotation("Autoincrement", true),
.Annotation("Sqlite:Autoincrement", true),
Url = table.Column<string>(nullable: true)
},
constraints: table =>
Expand All @@ -26,7 +26,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
columns: table => new
{
PostId = table.Column<int>(nullable: false)
.Annotation("Autoincrement", true),
.Annotation("Sqlite:Autoincrement", true),
BlogId = table.Column<int>(nullable: false),
Content = table.Column<string>(nullable: true),
Title = table.Column<string>(nullable: true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using EFGetStarted.UWP;
using Microsoft.EntityFrameworkCore.Storage;
using System;

namespace EFGetStarted.UWP.Migrations
{
Expand All @@ -12,8 +13,9 @@ partial class BloggingContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "1.0.0-rtm-21431");
.HasAnnotation("ProductVersion", "2.0.1");

modelBuilder.Entity("EFGetStarted.UWP.Blog", b =>
{
Expand Down Expand Up @@ -52,6 +54,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasForeignKey("BlogId")
.OnDelete(DeleteBehavior.Cascade);
});
#pragma warning restore 612, 618
}
}
}
Loading

0 comments on commit 0db6754

Please sign in to comment.