在本教學課程中,您會建立 Xamarin.Forms 應用程式,以使用 Entity Framework Core 對 SQLite 資料庫執行數據存取。
您可以在 Windows 或 Visual Studio for Mac 上使用 Visual Studio 來遵循本教學課程。
提示
您可以在 GitHub 上檢視本文的範例。
先決條件
安裝下列其中一項:
-
Visual Studio 2019 16.3 版或更新版本 搭配此工作負載:
- 使用 .NET 進行行動裝置開發
- Visual Studio for Mac
此 文件提供每個平臺 的詳細逐步安裝說明。
下載並執行範例專案
若要執行並探索此範例應用程式,請在 GitHub 上下載程式代碼。
下載之後,請在 Visual Studio 或 Visual Studio for Mac 中開啟方案檔 EFGettingStarted.sln,然後在您選擇的平臺上執行應用程式。
當應用程式第一次啟動時,它會初始化本機的 SQLite 資料庫,並填入兩個代表部落格的條目。
** 單擊工具列中的 新增 按鈕。
隨即會出現新的頁面,可讓您輸入新部落格的相關信息。
的螢幕快照
填寫所有資訊,然後在工具列上單擊 儲存。 新的部落格會儲存至應用程式的 SQLite 資料庫,並顯示在清單中。
您可以點擊清單中的其中一個部落格文章,並查看該部落格的所有文章。
的螢幕快照
按一下工具列中的 新增。
接著會出現一個頁面,可讓您填寫新部落格文章的相關信息。
的螢幕快照
填寫所有資訊,然後在工具列中按一下 [儲存]。
新文章會與您在上一個步驟中按兩下的部落格文章相關聯,並將儲存至應用程式的 SQLite 資料庫,並顯示在清單中。
返回部落格清單頁面。 按下工具列中的 刪除所有。 然後,所有部落格及其對應的文章都會從應用程式的 SQLite 資料庫中刪除。
探索程序代碼
下列各節將逐步引導您在範例專案中使用 EF Core 搭配 Xamarin.Forms 讀取、建立、更新和刪除 SQLite 資料庫中的程式代碼。
假設您已熟悉 顯示數據 的 Xamarin.Forms 主題,並 在頁面之間瀏覽。
重要
Entity Framework Core 會使用反射來叫用函式,而這些函式可能會在 Xamarin.iOS 連結器於 發行模式 組態時被去除。 您可以透過兩種方式之一來避免這種情況。
- 第一個是將
--linkskip System.Core新增至 其他 mtouch 自變數 在 iOS 建置 選項中。 - 或者,將 Xamarin.iOS Linker 行為 設定為 iOS 組建 選項中的
Don't Link。 本文將進一步說明 Xamarin.iOS 連結器,包括如何在 Xamarin.iOS 上設定行為。 (這種方法並不理想,因為它可能會導致商店拒絕)。
Entity Framework Core NuGet 套件
若要使用 EF Core 建立 Xamarin.Forms 應用程式,您要將 EF Core 資料庫提供者的套件安裝到 Xamarin.Forms 解決方案中的所有專案。 本教學課程使用 SQLite 提供者。
Xamarin.Forms 解決方案中的每個專案都需要下列 NuGet 套件。
Microsoft.EntityFrameworkCore.Sqlite
模型類別
透過EF Core 存取之 SQLite 資料庫中的每個數據表都會在類別中建立模型。 在此範例中,會使用兩個類別:Blog 和 Post,可在 Models 資料夾中找到。
模型類別僅由表示資料庫中欄位的屬性所組成。
Blog.cs
using System; using System.Collections.Generic; namespace EFGetStarted { public class Blog { public int BlogId { get; set; } public string Url { get; set; } public List<Post> Posts { get; set; } = new List<Post>(); } }Posts屬性會定義Blog與Post之間的父子關聯性。Post.cs
using System; namespace EFGetStarted { 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; } } }BlogId和Blog屬性與作為Post的實例的父級Blog物件相關。
數據內容
BloggingContext 類別位於 Services 資料夾中,並繼承自EF Core DbContext 類別。
DbContext 可用來將資料庫查詢和變更分組在一起。
using System;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Xamarin.Essentials;
namespace EFGetStarted
{
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public BloggingContext()
{
SQLitePCL.Batteries_V2.Init();
this.Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string dbPath = Path.Combine(FileSystem.AppDataDirectory, "blogs.db3");
optionsBuilder
.UseSqlite($"Filename={dbPath}");
}
}
}
- 此類型
DbSet的這兩個屬性都用來在代表部落格和貼文的基礎表上操作。 - 建構函式中需要
SQLitePCL.Batteries_V2.Init(),才能在 iOS 上起始 SQLite。 -
OnConfiguring函式會設定實體裝置上 SQLite 資料庫的位置。
建立、讀取、更新、刪除 &
以下是 EF Core 用來存取 SQLite 之應用程式中的一些實例。
讀
傳回所有記錄。
BlogsPage.xaml.cs的OnAppearing函式會傳回所有Blog記錄,並將其儲存到List變數中。using (var blogContext = new BloggingContext()) { var theBlogs = blogContext.Blogs.ToList(); }
傳回特定記錄。
PostsPage.xaml.cs的OnAppearing函式會傳回包含特定BlogId的Post記錄。using (var blogContext = new BloggingContext()) { var postList = blogContext.Posts .Where(p => p.BlogId == BlogId) .ToList(); }
創造
- 插入新記錄。
AddBlogPage.xaml.cs的Save_Clicked函式會將新的Blog物件插入 SQLite 資料庫中。var blog = new Blog { Url = blogUrl.Text }; using (var blogContext = new BloggingContext()) { blogContext.Add(blog); await blogContext.SaveChangesAsync(); }
更新
- 更新現有的記錄。
AddPostPage.xaml.cs的Save_Clicked函式會以新的Post更新現有的Blog物件。var newPost = new Post { BlogId = BlogId, Content = postCell.Text, Title = titleCell.Text }; using (var blogContext = new BloggingContext()) { var blog = await blogContext .Blogs .FirstAsync(b => b.BlogId == BlogId); blog.Posts.Add(newPost); await blogContext.SaveChangesAsync(); }
刪除
- 刪除所有記錄,並對子記錄進行連鎖刪除。
BlogsPage.xaml.cs的DeleteAll_Clicked函式會刪除 SQLite 資料庫中的所有Blog記錄,並將刪除串連至所有Blog子Post記錄。using (var blogContext = new BloggingContext()) { blogContext.RemoveRange(blogContext.Blogs); await blogContext.SaveChangesAsync(); }
後續步驟
在本快速入門中,您已瞭解如何使用 Xamarin.Forms 應用程式,以使用 Entity Framework Core 存取 SQLite 資料庫。