前言
本文中,您將創建一個通用應用程序(UWP),使用Entity Framework Core(Entity Framework 7)框架在SQLite數據庫上執行基本的數據訪問。
准備:
Entity Framework Core(Entity Framework 7)下文將簡稱:EF
1.在UWP中使用EF需要更新Microsoft.NETCore.UniversalWindowsPlatform到大於“5.2.2”的版本。
2.直接在“程序包管理器控制台”輸入命令來更新:Update-Package Microsoft.NETCore.UniversalWindowsPlatform
如果沒有的話下圖打開:建議保留在上圖的位置
安裝EF:
1.同樣使用命令來安裝:Install-Package Microsoft.EntityFrameworkCore.Sqlite
2.應為我們以后維護數據也得用EF,所以還要安裝工具包:Install-Package Microsoft.EntityFrameworkCore.Tools –Pre
創建數據模版
在項目上添加一個或多個Class文件,這些Class類就是最后生成的數據庫表。
1.數據表代碼
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Dome.UWP.Models { /// <summary> /// 設置數據表 /// </summary> [Table(name: "AccountBook")] class AccountBook { /// <summary> /// 設置編號列:不能為空、主鍵 /// </summary> [Required,Key] public int 編號 { get; set; } public decimal 金額 { get; set; } public DateTime 日期 { get; set; } public string 備注 { get; set; } public string 收支類型 { get; set; } public string 標簽名稱 { get; set; } public string 標簽圖標 { get; set; } public string 標簽備注 { get; set; } public string 賬戶名稱 { get; set; } } }
2.說明
代碼中的[Table(name:"AccountBook")]指定數據表的名稱,如果不指定的話會默認使用你定義的數據上下文中的屬性命名表了(下文會提到),這里是演示下。
中文字段?不用在意這些細節。
還有編號public int 編號 { get; set; }上的[Required,Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)],前面2個在代碼中有說明主要說下最后一個:
它的意思是就是“自增”的意思。
到這數據模型就創建完成了。
創建數據上下文
1.數據上下文代碼
using jizhang.UWP.Models; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Data.Common; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Dome.UWP.Services { class EFdbContext:DbContext { public DbSet<AccountBook> books { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { //配置數據庫名 optionsBuilder.UseSqlite("FileName=jizhang.db"); } } }
2.說明
這個代碼很少,主要說下:public DbSet<AccountBook> books { get; set; }如果你在數據模型中沒有指定表名的話就會使用這個名字來命名數據表。
還有就是你創建了多少個表就應該在這聲明多少個這個格式屬性,如果你不聲明的話最后在數據庫中不會出現沒有聲明的數據表了。
創建數據庫
Warning:本文現在為止在UWP項目中使用EF Tools還是不能正常工作的。需要手動添加binding redirects(綁定重定向).
1.在項目中添加一個文本文件。命名為App.config
2.向文件中添加如下類容:
<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>
3.好了現在有了一個數據模型,你就可以使用migrations 來生成數據庫文件了。
會在你的項目中自動添加一個migrations 文件夾
1)使用命令來生成:Add-Migration MyFirstMigration
MyFirstMigration你可以隨意寫,就是個名字罷了。(在這一步前最好編譯一次項目,減少出錯的幾率。)
這一步命令在哪輸入?小白都是懵逼的,就在上文用來安裝EF框架的那個“程序包控制台”里輸入就行了
2)如果你的數據模型改變了那么你得重新執行這個命令來更新數據庫。
4.由於我們是在APP運行的設備上使用數據庫,所以得在運行設備上創建數據庫。不用擔心會多次創建數據庫只會在第一次運行的時候創建。
那我們要怎么把創建的MyFirstMigration轉到數據庫中呢?
5.你會發現編譯過后還是沒有數據庫文件<數據的位置>(%USERPROFILE%\AppData\Local\Packages\)
1)你會發現打開是這樣的,簡直是淚奔不可能每次找APP的東西都去打開清單文件復制包名。
2)所以建議在項目創建的時候改掉包名,並且把這個Packages文件夾創建快捷方式放桌面。
6.最后我們打開APP.xaml.cs文件在構造函數中添加下面的代碼。
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 EFdbContext()) { db.Database.Migrate(); } } }
1.好了我們的數據庫就創建好了。
總結
差點忘了說了,如果你不執行Add-Migration MyFirstMigration命令,其他的工作你都做了那么最后也會生成數據庫文件但是只有一個__EFMigrationsHistory表
下一篇我再說具體操作數據。