自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何使用 Entity Framework 的 DbContext

開源
微軟的 Entity Framework 是一個開源的 對象-關(guān)系映射 ORM 框架,它幫助我們打通了 數(shù)據(jù)庫的數(shù)據(jù)模型 到 代碼層的領(lǐng)域模型,Entity Framework 簡化了應(yīng)用程序?qū)?shù)據(jù)庫的 CURD 操作,而且還向高層屏蔽了數(shù)據(jù)是如何持久化到數(shù)據(jù)庫的。

[[380644]]

本文轉(zhuǎn)載自微信公眾號「碼農(nóng)讀書」,作者碼農(nóng)讀書 。轉(zhuǎn)載本文請聯(lián)系碼農(nóng)讀書公眾號。

微軟的 Entity Framework 是一個開源的 對象-關(guān)系映射 ORM 框架,它幫助我們打通了 數(shù)據(jù)庫的數(shù)據(jù)模型 到 代碼層的領(lǐng)域模型,Entity Framework 簡化了應(yīng)用程序?qū)?shù)據(jù)庫的 CURD 操作,而且還向高層屏蔽了數(shù)據(jù)是如何持久化到數(shù)據(jù)庫的。

說的具體一點就是 DbContext 充當(dāng)了數(shù)據(jù)庫到領(lǐng)域模型之間的橋梁,這篇文章我們將會討論如何配置 DbContext 并使用 Entity Framework Core provider 對數(shù)據(jù)庫進行 CURD 操作。

DbContext

DbContext 是 EF 中非常重要的一個組件,它扮演著 Database 的會話連接,使用它可以查詢數(shù)據(jù)到你的 entitys 集合中,也可以通過它將 entitys 保存到底層數(shù)據(jù)庫中, EntityFramework Core 中的 DbContext 擁有如下幾個功能模塊。

  • 連接管理
  • 查詢數(shù)據(jù)
  • 持久化數(shù)據(jù)
  • 修改跟蹤
  • 緩存
  • 事務(wù)管理

要想使用 EntityFramework,需要通過 nuget 引用 Microsoft.EntityFrameworkCore 包,可以通過 Visual Studio 2019 的 NuGet package manager 可視化界面安裝 或者 通過 NuGet package manager 命令行工具輸入以下命令:

  1. dotnet add package Microsoft.EntityFrameworkCore 

接下來討論下如何在 ASP.Net Core 中使用 DbContext 。

創(chuàng)建 DbContext

首先創(chuàng)建一個 CustomContext 類,并繼承 Entity Framework 中的基類 DbContext,如下代碼所示:

  1. public class CustomContext : DbContext 
  2.     { 
  3.         public CustomContext(DbContextOptions options) : base(options) 
  4.         { 
  5.         } 
  6.  
  7.         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
  8.         { 
  9.            //Write your code here to configure the context 
  10.         } 
  11.  
  12.         protected override void OnModelCreating(ModelBuilder modelBuilder) 
  13.         { 
  14.             //Write your code here to configure the model 
  15.         } 
  16.     } 

可以看到 CustomContext 的構(gòu)造函數(shù)中接受了 DbContextOptions 類型的參數(shù),該類主要用于對 DbContext 做一些必要的參數(shù)配置,當(dāng)然你也可以在 OnConfiguring() 中對 DbContext 進行配置,接下來的 OnModelCreating() 方法用于對 model 進行配置。

下面我在 CustomContext 中新增幾個 DbSet 屬性用來表示實體集合,如下代碼所示:

  1. public class CustomContext : DbContext 
  2.     { 
  3.         public CustomContext(DbContextOptions options) : base(options) 
  4.         { 
  5.         } 
  6.  
  7.         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
  8.         { 
  9.         } 
  10.  
  11.         protected override void OnModelCreating(ModelBuilder modelBuilder) 
  12.         { 
  13.         } 
  14.  
  15.         public DbSet<Author> Authors { get; set; } 
  16.  
  17.         public DbSet<Blog> Blogs { get; set; } 
  18.     } 
  19.  
  20.     public class Author 
  21.     { 
  22.         public int AuthorID { get; set; } 
  23.  
  24.         public string AuthorName { get; set; } 
  25.     } 
  26.  
  27.     public class Blog 
  28.     { 
  29.         public int BlogID { get; set; } 
  30.  
  31.         public string BlogName { get; set; } 
  32.  
  33.         public int AuthorID { get; set; } 
  34.     } 

注冊 DbContext 注入到 ASP.NET Core 運行時

要想在 ASP.NET Core 中使用,需要將 CustomerContext 注入到 ServiceCollection 容器中,這里采用 SqlServer 作為底層存儲,所以還需要在 NuGet 上引用 Microsoft.EntityFrameworkCore.SqlServer 包,接下來在 Startup.ConfigureServices() 中新增如下代碼:

  1. public class Startup 
  2.     { 
  3.         // This method gets called by the runtime. Use this method to add services to the container. 
  4.         public void ConfigureServices(IServiceCollection services) 
  5.         { 
  6.             services.AddControllersWithViews(); 
  7.  
  8.             services.AddDbContext<CustomContext>(options => options.UseSqlServer("Data Source=.; Initial Catalog=MyTest; Trusted_Connection=Yes")); 
  9.         } 
  10.     } 

DbContext 依賴注入

現(xiàn)在 CustomContext 已經(jīng)注入到容器了,接下來就可以在 HomeController 中通過依賴注入的方式獲取 CustomerContext 實例,下面的代碼片段展示了如何去實現(xiàn)。

  1. public class HomeController : Controller 
  2.     { 
  3.         ILogger<HomeController> logger; 
  4.         private CustomContext dbContext; 
  5.  
  6.         public HomeController(ILogger<HomeController> logger, CustomContext dbContext) 
  7.         { 
  8.             this.logger = logger; 
  9.             this.dbContext = dbContext; 
  10.  
  11.             dbContext.Database.EnsureCreated(); 
  12.         } 
  13.     } 

上面的代碼,我用了 dbContext.Database.EnsureCreated(); 來確保數(shù)據(jù)庫已經(jīng)成功創(chuàng)建,執(zhí)行完這句代碼之后,數(shù)據(jù)庫將會生成 MyTest 數(shù)據(jù)庫 和 Author,Blog 兩張表結(jié)構(gòu),如下圖所示:

接下來在 Index 方法中插入一條記錄并查詢,效果如下:

這就是配置 EF 所要做的所有事情,現(xiàn)在你可以利用 CustomContext 去所 CURD 操作了,DbContext 在概念上類似 ObjectContext,表示一個 UnitOfWork 組合單元,并且 EF 是DDD領(lǐng)域的一個實現(xiàn)案例,DbContext 的職責(zé)就是負(fù)責(zé) 應(yīng)用程序 和 數(shù)據(jù)庫 之間的交互,關(guān)于 Entity Framework Core 的更多特性,我會放到后面的文章中和大家一起分享。

譯文鏈接:https://www.infoworld.com/article/3311737/how-to-use-the-dbcontext-in-entity-framework-core.html

 

責(zé)任編輯:武曉燕 來源: 碼農(nóng)讀書
相關(guān)推薦

2012-07-20 10:38:25

Entity FramEF

2024-06-18 12:58:12

2016-08-16 09:06:07

Entity FramT4模板sql語句

2010-02-23 14:15:26

Entity Fram

2010-04-20 09:40:38

ADO.NET Ent

2019-11-13 15:40:00

Entity Fram審計數(shù)據(jù)數(shù)據(jù)庫

2012-08-24 09:24:06

線程DbContext

2019-11-21 09:00:00

數(shù)據(jù)驗證EF Core

2024-03-26 12:28:59

.NET 8開發(fā)

2009-12-22 16:03:34

ADO.NET Ent

2024-12-05 09:06:14

ORM框架.NET

2009-11-03 16:27:43

ADO.NET Ent

2009-12-30 14:03:36

ADO.NET Ent

2009-12-22 14:46:09

ADO.NET Ent

2012-09-10 10:23:38

Entity Fram

2009-09-04 14:52:03

ADO.NET Ent

2024-09-23 00:00:00

數(shù)據(jù)庫場景Entity

2009-12-23 16:00:50

ADO.NET Ent

2009-12-22 17:14:37

ADO.NET Ent

2009-12-30 09:10:04

ADO.NET Ent
點贊
收藏

51CTO技術(shù)棧公眾號