📜 ⬆️ ⬇️

Use the Entity Framework Core with the universal Windows platform application


You know that UWP has a rather special data handling principle. This is because the main way to distribute applications is to download from the Store. In addition, the very versatility of the platform implies that the OS can be installed on devices of various types. And, say, SQL Server is not yet installed on the phone. In addition to REST services, the only available database format is SQLite. The good news is that the format is quite popular. There are several wrapper libraries for working with databases of this format. Well, here, with the release of .Net Core, under the UWP, work with the Entity Framework Core becomes available. Could not resist and decided to write about it.

I suggest testing EF Core in practice and creating a simple UWP application with a guest book. There is no practical sense in a local database with a guest book, but for a simple example, the most. If desired, in real projects you can synchronize with an external database.

First, let's create a UWP application or open an existing application. Logging into the NuGet package manager will most likely find that the Microsoft .NETCore.UniversalWindowsPlatform library needs to be updated.


')
Entity Framework Core only works with a version older than 5.2.2. You can update by clicking on the Install button or by running the NuGet package manager console command.

Update-Package Microsoft.NETCore.UniversalWindowsPlatform 

After the upgrade, you can install the EF Core itself . Either by searching the package manager NuGet using the phrase Microsoft.EntityFrameworkCore.Sqlite or by typing:

 Install-Package Microsoft.EntityFrameworkCore.Sqlite 

In addition, you must install the Microsoft.EntityFrameworkCore.Tools tools .
For now this is a prerelease, and you can install the tools with the command NuGet PM:

 Install-Package Microsoft.EntityFrameworkCore.Tools –Pre 

If you installed the version of the tools 1.0.0-preview2 (and at the time of writing this is the latest version), you need to make the following fix:

Create an App.config file in the project root with the following content:

 <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> 

Now you can add the Model.cs class with the data model. For example, I created the following simplest model:

  public class MessagingContext : DbContext { public DbSet<Message> Messages { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite("Filename=Guestbook.db"); } } public class Message { public int MessageId { get; set; } public string MessageText { get; set; } } 

Here we have the Message class for guestbook entries and the MessagingContext class inherited from DbContext, which overrides the OnConfiguring method, specifying the name of the database file. Intellisense should remind you with underscores that you need to add the Microsoft.EntityFrameworkCore namespace.

Is done. You can run the migration to create helper classes. From the console package manager NuGet run the command:

 Add-Migration MyGuestbookMigration 

where MyGuestbookMigration is an arbitrary name for the migration class being created.
A successful message will inform you of a message indicating that you must use the Remove-Migration command to cancel. A Migrations folder will be created with a couple of classes inside.

I also had the following error:
Add-Migration: Exception calling "CreateInstanceAndUnwrap" with "8" argument (s): "Could not load file
or assembly 'Microsoft.EntityFrameworkCore, Version = 1.0.0.0, Culture = neutral,
PublicKeyToken = adb9793829ddae60 'or one of its dependencies. The located assembly's manifest definition
does not match the assembly reference. (Exception from HRESULT: 0x80131040) "
At line: 1 char: 1
+ Add-Migration MyFirstMigration
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo: NotSpecified: (:) [Add-Migration], MethodInvocationException
+ FullyQualifiedErrorId: FileLoadException, Add-Migration

Which made add a couple more of the following bindings in the App.config:

  <dependentAssembly> <assemblyIdentity name="Microsoft.EntityFrameworkCore" publicKeyToken="adb9793829ddae60" culture="neutral" /> <bindingRedirect oldVersion="1.0.0.0" newVersion="1.0.1.0"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.EntityFrameworkCore.Relational" publicKeyToken="adb9793829ddae60" culture="neutral" /> <bindingRedirect oldVersion="1.0.0.0" newVersion="1.0.1.0"/> </dependentAssembly> 

Preview what you take from it. Now we need to add code in App.xaml.cs, which will apply all new migrations to the database each time the application is opened. When you first open the application, the database will be created.

Add 4 lines of code to the end of the constructor:

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

well and besides this namespace:

 using Microsoft.EntityFrameworkCore; 

The created database file will be found in the LocalState directory.
The easiest way to find it is through the device portal.

Well, or, as usual, by opening the folder of the application, which is usually in the directory C: \ Users \ {username} \ AppData \ Local \ Packages \
To display guestbook entries, add a ListView with a binding to MainPage.xaml:

  <ListView Name="Guestbook"> <ListView.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding MessageText}" /> </DataTemplate> </ListView.ItemTemplate> </ListView> 

Add the Loaded event and the handler to the page itself:

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

If we manually add entries to the database file, they will be displayed.
But it is better, of course, to add somewhere a text field with a button and a button click event handler:

  <TextBox Name="txtNewPost"></TextBox> <Button Click="Add_Post_Click"> </Button> 

and

  private void Add_Post_Click(object sender, RoutedEventArgs e) { using (var db = new MessagingContext()) { var post = new Message { MessageText = txtNewPost.Text }; db.Messages.Add(post); db.SaveChanges(); Guestbook.ItemsSource = db.Messages.ToList(); } } 

For experienced developers, this is all rather news or a review of a new available feature than a tutorial, but for those who are just starting, I’ll give you an additional example of updating and deleting records:

  using (var db = new MessagingContext()) { var messagesList = db.Messages.ToList<Message>(); // Update Message messageToUpdate = messagesList.Where(m => m.MessageId == 1).FirstOrDefault<Message>(); messageToUpdate.MessageText = " "; //     // Delete if (messagesList.Count >= 2) db.Messages.Remove(messagesList.ElementAt<Message>(1)); //    db.SaveChanges(); } 

»Official English manual: Local SQLite on UWP

Source: https://habr.com/ru/post/310516/


All Articles