前言
之前UWP課程中曾經配置了一下EFCORE數據庫,當時爬這個坑爬了將近能有三天,現在整理一下配置順序,原來用來記錄的txt文件是用英文記錄的,這邊稍微整理一下吧。
Notes :
- Updated 2020.01.14
- 現在微軟的EntityFrameWork3.0並不支持UWP新數據庫的使用,該博客內容更新爲 —— 如何降低版本( 使用 EntityFrameWorkCore 2.2 )搭建UWP數據庫
- 開發環境適配要求 : VisualStudio版本大於2015 (本人在VS2019中進行了搭建,親測可行)
- 文檔所維護的版本 :
EntityFrameWork Core 2.2
- Updated 2018.07.15
- 建議配合官方文檔食用
- 開發環境適配要求
- VisualStudio 2015/2017
- dotnet core version ≥ 2.0
- dotnet standard version ≥ 2.0
正文
Part1 : 數據庫的創建
- 創建一個UWP解決方案
- 在你的項目中創建一個
.net Standard
文件,同時將它作爲解決方案中的啓動項目(set it as the ‘StartUP’ project under the same solution)- 請注意,除了設置爲啓動項目外,也需要在nuget管理器界面中,將默認啓動項目調整爲你的數據庫項目!
例 : (EasyChat.DataBase
爲.net Standard
項目)
- 請注意,除了設置爲啓動項目外,也需要在nuget管理器界面中,將默認啓動項目調整爲你的數據庫項目!
- 打開Nuget管理器,在控制檯執行(Update 2020.01.14)
Install-Package NETStandard.Library -Version 2.0.3
Install-Package Microsoft.EntityFrameWorkCore.Sqlite -Version 2.2.0
Install-Package Microsoft.EntityFrameWorkCore.tools -Version 2.2.0
- 右鍵新創建的EFCORE文件中項目根目錄(位置如下圖),點擊“編輯.csproj文件”
在其中的<TargetFrameworks> 處,ctrl+c & ctrl+v以下代碼(覆蓋該對應行)
<TargetFrameworks>netcoreapp2.0;netstandard2.0</TargetFrameworks>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
- net中的數據庫與一般的E-R數據庫有點類似,數據以類爲單位存儲,每個類成員(字段)對應的就是我們關係型數據庫中的每個表的表項,此處僅以學生-選課-課程模型中的學生表爲例:
//student.class
using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
namespace DB
{
public class Student
{
public int ID { get; set; }
public string Number { get; set; }
public string Name { get; set; }
public int grade { get; set; }
}
public class StudentContext : DbContext
{
public DbSet<Student> Students { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
builder.UseSqlite("Data Source = student.db");
}
}
}
-
打開控制檯,在控制檯依次執行
Add-Migration [MigrationName]
- PS :
[MigrationName]
意爲可以隨便命名,如Add-Migration helloworld
- PS :
-
重新打開我們剛剛打開的.csproj文件,將剛剛我們修改的<TargetFrameworks> 標籤修改爲
<TargetFrameworks>netstandard2.0</TargetFrameworks>
- 注意 :<GenerateRuntimeConfigurationFiles> 標籤對不需要修改
-
以上,我們完成了數據庫的搭建,接下來的部分,我們將開始在UWP數據庫中對其進行引用
-
首先,將UWP程序作爲解決方案中的啓動項目
-
( Update 2020.01.14 ) 打開Nuget管理器,將默認項目更改爲UWP,在控制檯執行:
Install-Package Microsoft.EntityFrameWorkCore -Version 2.2.0
-
在UWP項目中添加對.net項目的引用
-
添加以下代碼於app.xaml.cs(需添加處增加了備註
// Add
):using Microsoft.EntityFrameworkCore; //Add using System; using Windows.ApplicationModel; using Windows.ApplicationModel.Activation; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace EFGetStarted.UWP { /// <summary> /// Provides application-specific behavior to supplement the default Application class. /// </summary> sealed partial class App : Application { /// <summary> /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). /// </summary> public App() { this.InitializeComponent(); this.Suspending += OnSuspending; using (var db = new studentContext()) //Add { //Add db.Database.EnsureCreated();// Add }// Add } ...
PART2 : 在UWP中進行增刪查改
-
將UWP項目設爲啓動項目(set it as the ‘start-up’ project)
-
增刪查改樣例代碼如下 :
//新增數據 private async void add_Click(object sender, RoutedEventArgs e) { using (var db = new studentContext()) { Student s = new Student { ID = (int) DateTime.Now.Ticks, Name = name.Text, Number = number.Text, grade = int.Parse(score.Text) }; db.Students.Add(s); await db.SaveChangesAsync(); } } //查詢數據 private async void query_Click(object sender, RoutedEventArgs e) { using (var db = new studentContext()) { member.Text = (await db.Students.CountAsync()).ToString(); } } //數據刪除 private void delete_Click(object sender, RoutedEventArgs e) { using (var db = new studentContext()) { db.Database.Migrate(); } }
-
開始你的新數據庫使用之旅吧!
技術文檔補充 :
- Migration後如果對數據類型進行更改,按照普通的
remove-migration
和update-database
操作,會導致你再重新建立Migration時,EntityFrameWork Core仍沿用之前所建立的Migration進行數據庫搭建,從而造成數據庫出錯。這裏是一些關於數據庫重新搭建的建議,僅供參考 : - 本文檔所提供的樣例搭建,爲簡單數據類型的樣例搭建,如果考慮複雜數據類型的數據庫類型生成,以及各個表之間的關聯關係,您可以參考UWP, ASP.NET Core等其他開發平臺對於該問題的解決方案和GuideLine。由於同樣使用EntityFrameWork Core,所以實現方法是一樣的。下面是一些可能有效的技術文檔,僅供參考 :
References:
- MicroSoft官方文檔 (Updated 2020.01.04 : 現已變爲ASP.NET Core的搭建GuideLines)
- Special Thanks to the first user giving feedback under the official document, though the feedbacks under the document is closed now without a reason why 😦
- My Teacher, Professor Zhang Yin