UWP: 在 UWP 中使用 Entity Framework Core 操作 SQLite 数据库

简介: 原文:UWP: 在 UWP 中使用 Entity Framework Core 操作 SQLite 数据库在应用中使用 SQLite 数据库来存储数据是相当常见的。在 UWP 平台中要使用 SQLite,一般会使用 SQLite for Universal Windows Platform 和 SQLite PCL 之类的库,前者是 SQLite 引擎库,而后者则提供了用于操作数据库的 API ,不过自从 Windows Fall Creators Update 之后,我们有了新的选择。
原文: UWP: 在 UWP 中使用 Entity Framework Core 操作 SQLite 数据库

在应用中使用 SQLite 数据库来存储数据是相当常见的。在 UWP 平台中要使用 SQLite,一般会使用 SQLite for Universal Windows Platform 和 SQLite PCL 之类的库,前者是 SQLite 引擎库,而后者则提供了用于操作数据库的 API ,不过自从 Windows Fall Creators Update 之后,我们有了新的选择。

由于 UWP 在其 Windows Fall Creators Update SDK 中增加对 .NET Standard 2.0 的支持,并且 Entity Framework Core 2.0(以下简称 EF Core)也支持 .NET Standard 2.0,这就使得我们能在 UWP 应用中使用 EF Core 来操作 SQLite 数据库。相比前者,使用 EF Core 最明显的优点是可以使用 Entity Framework 的特性(如 Fluent API、Migration 等);此外,由于 EF Core 实现了 .NET Standard 并且还在继续迭代过程中,这些方面都能够成为我们使用 EF Core 的原因。

接下来,我们将会通过一个简单的例子来看如何在 UWP 中使用 EF Core,在开始之前,你的环境必须满足以下几个条件:

  • Windows 10 Fall Creators Update (10.0.16299.0);
  • 安装了 .NET Core 2.0 SDK(或更高版本);
  • Visual Studio 2017 的版本为 15.4 或更高;

实现

1. 项目创建

创建一个 UWP 项目,名为 LSNote,这是一个可以管理笔记的应用。注意:其最小版本,应该为 Windows Fall Creators Update,如下:

然后,在解决方案中添加一个 .NET Standard 项目,名为 LSNote.Model,我们要在这个项目中添加一些 Model。接着,为 UWP 项目添加对这个 .NET Standard 项目的引用。最终的项目结构如下:

需要说明的是:如果不使用 EF Core 的 Migration 功能,则不需要再创建后面的 .NET Standard 项目(后面我们会提到 Migration);另外,将 Model 单独放在一个项目中,也可以使它与其它实现 .NET Standard 的项目共享,如 ASP.net Core 或 WPF/WinForm(目标框架应为 .NET Framework 4.6.1 或更高)。

2.  Model 项目编码与配置

首先,为它添加对 EF Core 的引用,通过 NuGet,添加对以下两个包的引用: 

Install-Package Microsoft.EntityFrameworkCore.Sqlite
Install-Package Microsoft.EntityFrameworkCore.Tools

其中,第一个是 EF Core 库本身,而二个提供了用于 Migration 的工具。

这里简单解释一下 Migration(一译,迁移),它以增量的方式来修改数据库以及表结构,使 EF Core 的模型与数据库保持一致,并且不会影响数据库中现有的数据。

接下来,在 LSNote.Model 项目中,添加以下代码,其中包括三个类:

using Microsoft.EntityFrameworkCore;

namespace LSNote.Model
{
    public class Note
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public Category Category { get; set; }
    }

    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class NoteDbContext : DbContext
    {
        public DbSet<Category> Categories { get; set; }

        /// <summary>
        /// 数据库文件的路径
        /// </summary>
        public string DbFilePath { get; set; }

        public DbSet<Note> Notes { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);

            // 设置数据库文件的路径
            optionsBuilder.UseSqlite($"Data Source={DbFilePath}");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 必填
            modelBuilder.Entity<Category>().Property(m => m.Name).IsRequired();
            modelBuilder.Entity<Note>().Property(m => m.Title).IsRequired();
        }
    }
}

其中:

  • Note:表示一个笔记项;
  • Category: 表示一个类别,即笔记的类别;
  • NoteDbContext: 这个类继承自 DbContext,它里面包括了若干了 DbSet<T> 的属性,代表对应的数据表;

此外在 NoteDbContext 中,我们重载的两个方法意义分别如下:

  • OnConfiguring: 配置数据库,每当 DbContext 实例被创建时,它都会被执行;
  • OnModelCreating: 配置 Model 及其属性,并最终影响数据表以及字段;

然后,为了使用 Migration,我们还要编辑 LSNote.Model 项目的属性(编辑 LSNote.Model.csproj):

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>netcoreapp2.0;netstandard2.0</TargetFrameworks>  
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="microsoft.entityframeworkcore.sqlite" Version="2.0.1" />
    <PackageReference Include="microsoft.entityframeworkcore.tools" Version="2.0.1" />
  </ItemGroup>
</Project>

注意其中增加的加粗部分,我们使当前项目的目标框架为 .netcoreapp2.0 和 .netstandard2.0,另外增加了 GenerateRuntimeConfigurationFiles 节点。

此时,设置 LSNote.Model 为启动项目,然后在 PMC(Package Manager Console)中,确保 Defalut project 也是 LSNote.Model,之后,输入以下命令:

Add-Migration Init

其中 Init 是本次 Migration 的名称。之后,可以看到在项目中生成了一个 Migrations 文件夹,其下包括了代表每次 Migration 的类文件,它们继承自 Migration 类,如下:

注意:执行 Migration 命令,必须使 LSNote.Model 项目为启动项,这是因为目前版本的 EF Core Tools 还不支持 UWP 这种类型的项目。

3. 在 UWP 项目中使用

首先,为 UWP 项目也添加对 EF Core 的引用,执行命令:

Install-Package Microsoft.EntityFrameworkCore.Sqlite

接下来,在 App.xaml.cs 文件中,增加以下代码(加粗部分):

using Microsoft.EntityFrameworkCore;

        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;

            DbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "notes.db");

            try
            {
                using (var con = new LSNote.Model.NoteDbContext())
                {
                    con.DbFilePath = DbPath;

                    con.Database.Migrate();
                }
            }
            catch (NotSupportedException ex)
            {
            }
            catch (Exception ex)
            {
            }
        }

        public static string DbPath { get; set; }

通过 DatabaseFacade 类的 Migrate 方法,将挂起的 Migration 应用到数据库,如果数据库还没创建,它会先创建数据库;其中 DatabaseFacade 类由 con.Database 属性得到。

然后,我们在 MainPage.xaml.cs 中,添加以下代码:

    public sealed partial class MainPage : Page, INotifyPropertyChanged
    {
        private List<Note> _allNotes;

        public MainPage()
        {
            this.InitializeComponent();

            this.Loaded += MainPage_Loaded;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public List<Note> AllNotes
        {
            get { return _allNotes; }
            set
            {
                _allNotes = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(AllNotes)));
            }
        }

        private void MainPage_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            using (var con = new NoteDbContext())
            {
                // 设置数据库路径
                con.DbFilePath = App.DbPath;

                // 添加分类
                if (con.Categories.ToList().Count == 0)
                {
                    var cate1 = new Category { Name = "分类A" };
                    var cate2 = new Category { Name = "分类B" };
                    con.Categories.AddRange(cate1, cate2);
                    con.SaveChanges();
                }

                // 添加笔记
                con.Notes.Add(new Note { Title = "这是一条记录", Content = "一些备注", Category = con.Categories.First() });
                con.SaveChanges();

                // 查询
                AllNotes = con.Notes.ToList();
            }
        }
    }

在 MainPage.xaml 中添加以下代码:

<Page
    ...
    xmlns:model="using:LSNote.Model"
    ...

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView x:Name="list" ItemsSource="{x:Bind AllNotes, Mode=OneWay}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="model:Note">
                    <StackPanel Margin="0,4">
                        <TextBlock
                            FontSize="16"
                            FontWeight="SemiBold"
                            Text="{x:Bind Title}" />
                        <TextBlock Text="{x:Bind Content}" />
                        <TextBlock>
                            <Run Text="Category:" />
                            <Run Text="{x:Bind Category.Name}" />
                        </TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Page>

这时,将 UWP 项目设置为启动项目,按 F5 运行,即可看到结果:

4. 进一步使用 Migration

前面说过,使用 EF Core,我们可以使用它自身的特性,例如,当我们要对现有的数据库或表结构修改时,使用 Migration 将会非常简单。对于上面的 Note 实体,我们要为它新添加一个 IsDelete 属性,然后再通过 Add-Migration 命令和 Migrate 方法最终影响到数据库。

首先,添加属性:

    public bool IsDelete { get; set; }

接下来,仍然要设置 LSNote.Model 为启动项目,并且在 PMC 中确认 Defalut project 也是它,输入命令:

Add-Migration AddIsDeleteField

这时,在 Migrations 文件夹会新生成对应的类文件。

切换启动项目为 UWP 项目,并且在 MainPage.xaml 中增加以下代码(加粗部分):

    <DataTemplate x:DataType="model:Note">
        <StackPanel Margin="0,4">
            <TextBlock
                FontSize="16"
                FontWeight="SemiBold"
                Text="{x:Bind Title}" />
            <TextBlock Text="{x:Bind Content}" />
            <TextBlock>
                <Run Text="Category:" />
                <Run Text="{x:Bind Category.Name}" />
            </TextBlock>
            <TextBlock>
                <Run Text="IsDelete:" />
                <Run Text="{x:Bind IsDelete}" />
            </TextBlock>
        </StackPanel>
    </DataTemplate>

按 F5 运行,即可看到更新后的结果:

补充的话:关于 Migration

  • 对于 SQLite,EF Core 的 Migration 目前还有一些限制,并不能满足所有的功能,如在创建数据表后,再添加外键、添加主键,等。这里有一份完整的限制操作列表;如果在 Migration 中,包括这些操作,将会引发 NotSupportedException。不过,值得注意的是,随着 EF Core 的不断完善,在将来,这些限制都应该会一一实现;
  • 另外,建议每次在 Migration 前要备份数据

总结

本文主要讨论了在 UWP 中如何使用 EF Core,由于两者都依赖并且支持 .NET Standard 2.0,所以在 EF Core 能够被用于 UWP 中,并且运行在任何支持 Win 10 的设备上。接下来,如果你正在开发或者准备开发的应用会用到 SQLite 数据库,不妨试试 EF Core 。

 

参考资料:

Getting Started with EF Core on Universal Windows Platform

The Secret to Running EF Core 2.0 Migrations from a NET Core or NET Standard Class Library

 

源码下载

目录
相关文章
|
JavaScript 关系型数据库 MySQL
❤Nodejs 第六章(操作本地数据库前置知识优化)
【4月更文挑战第6天】本文介绍了Node.js操作本地数据库的前置配置和优化,包括处理接口跨域的CORS中间件,以及解析请求数据的body-parser、cookie-parser和multer。还讲解了与MySQL数据库交互的两种方式:`createPool`(适用于高并发,通过连接池管理连接)和`createConnection`(适用于低负载)。
18 0
|
30天前
|
API 数据库 C语言
【C/C++ 数据库 sqlite3】SQLite C语言API返回值深入解析
【C/C++ 数据库 sqlite3】SQLite C语言API返回值深入解析
169 0
|
1月前
|
SQL 数据库连接 数据库
你不知道ADo.Net中操作数据库的步骤【超详细整理】
你不知道ADo.Net中操作数据库的步骤【超详细整理】
16 0
|
12天前
|
SQL 关系型数据库 数据库
Python中SQLite数据库操作详解:利用sqlite3模块
【4月更文挑战第13天】在Python编程中,SQLite数据库是一个轻量级的关系型数据库管理系统,它包含在一个单一的文件内,不需要一个单独的服务器进程或操作系统级别的配置。由于其简单易用和高效性,SQLite经常作为应用程序的本地数据库解决方案。Python的内置sqlite3模块提供了与SQLite数据库交互的接口,使得在Python中操作SQLite数据库变得非常容易。
|
16天前
|
存储 关系型数据库 MySQL
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
|
16天前
|
关系型数据库 MySQL 数据库连接
Python+SQLite数据库实现服务端高并发写入
Python中使用SQLite内存模式实现高并发写入:创建内存数据库连接,建立表格,通过多线程并发写入数据。虽然能避免数据竞争,但由于SQLite内存模式采用锁机制,可能在高并发时引发性能瓶颈。若需更高性能,可选择MySQL或PostgreSQL。
23 0
|
1月前
|
缓存 NoSQL 数据库
[Redis]——数据一致性,先操作数据库,还是先更新缓存?
[Redis]——数据一致性,先操作数据库,还是先更新缓存?
|
1月前
|
SQL 存储 关系型数据库
【mysql】—— 数据库的操作
【mysql】—— 数据库的操作
【mysql】—— 数据库的操作
|
1月前
|
关系型数据库 数据库 C++
嵌入式数据库sqlite3【基础篇】基本命令操作,小白一看就懂(C/C++)
嵌入式数据库sqlite3【基础篇】基本命令操作,小白一看就懂(C/C++)
|
1月前
|
存储 SQL 数据库
django如何连接sqlite数据库?
django如何连接sqlite数据库?
45 0