Entity Framework Core延遲加載(懶加載)用法
眾所周知在EF 6 及以前的版本中,是支持懶加載(Lazy Loading)的,可惜在EF Core 并不支持,必須使用Include
方法來支持導(dǎo)航屬性的數(shù)據(jù)加載。不過現(xiàn)在EF Core的開發(fā)團(tuán)隊(duì)打算恢復(fù)對這一功能的支持(目前還未發(fā)布,不過可以在Github上面下載進(jìn)行測試)。
懶加載
懶加載也可以叫做按需加載、延遲加載??梢苑謨煞矫鎭砝斫?,一方面指暫時(shí)不需要該數(shù)據(jù),不用在當(dāng)前馬上加載,而可以推遲到使用它時(shí)再加載;另一方面指不確定是否將會需要該數(shù)據(jù),所以暫時(shí)請不要加載,待確定需要后再加載它。懶加載是一種很重要的數(shù)據(jù)訪問特性,可以有效地減少與數(shù)據(jù)源的交互(注意,這里所提的交互不是指交互次數(shù),而是指交互的數(shù)據(jù)量),從而提升程序性能。
EF 6 懶加載
我們先來看一看在EF 6中的懶加載的使用方式。
實(shí)體定義:
public class Order { public int OrderID { get; set; } public string CustomerID { get; set; } public DateTime? OrderDate { get; set; } public virtual ICollection<OrderDetail> OrderDetails { get; set; } } public class OrderDetail { public int OrderID { get; set; } public int ProductID { get; set; } public decimal UnitPrice { get; set; } public short Quantity { get; set; } public float Discount { get; set; } public virtual Order Order { get; set; } }
我們在這里定義訂單、訂單明細(xì)實(shí)體,它們是一對多關(guān)系,通過OrderId
字段進(jìn)行關(guān)聯(lián)。
using (NorthwindContext context = new NorthwindContext()) { Order order = await context.Orders.SingleAsync(item => item.OrderID == 10253); Assert.NotNull(order); Assert.NotNull(order.OrderDetails); Assert.Equal(3, order.OrderDetails.Count); } }
在查詢訂單號為 10253 的訂單后,如果我們需要訪問訂單的明細(xì),不需要再編寫一次數(shù)據(jù)查詢的代碼,直接訪問導(dǎo)航屬性即可,EF會自動(dòng)幫我們填充屬性的值。
懶加載需要注意以下兩點(diǎn):
- 在配置中啟用了懶加載(默認(rèn)開啟);
- 實(shí)體類不能是封閉(
sealed
)類,導(dǎo)航屬性必須是虛(virtual
)屬性。
在 EF Core 中啟用懶加載
目前EF Core發(fā)布的最新版本中并不支持懶加載,開發(fā)人員必須使用Include
方法,才能完成導(dǎo)航屬性的加載。
using (NorthwindContext context = new NorthwindContext()) { Order order = await context.Orders.Include(e => e.OrderDetails).SingleAsync(item => item.OrderID == 10253); Assert.NotNull(order); Assert.NotNull(order.OrderDetails); Assert.Equal(3, order.OrderDetails.Count); }
大家需要在Github上面下載最新的源代碼來測試這一功能 aspnet/EntityFrameworkCore。
啟用懶加載:
public class NorthwindContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var sqlConnectionStringBuilder = new SqlConnectionStringBuilder { DataSource = "****", InitialCatalog = "Northwind", UserID = "sa", Password = "sa" }; optionsBuilder.UseSqlServer(sqlConnectionStringBuilder.ConnectionString); optionsBuilder.UseLazyLoadingProxies(); base.OnConfiguring(optionsBuilder); } }
要在通常的應(yīng)用程序中使用,只需在DbContext
的OnConfiguring
方法中添加對UseLazyLoadingProxies()
擴(kuò)展方法調(diào)用即可。
框架目前是通過Castle.Core
框架來生成代理類來實(shí)現(xiàn)對導(dǎo)航屬性的延遲加載,開發(fā)團(tuán)隊(duì)打算將該功能做為EF Core的可選安裝包。
到此這篇關(guān)于Entity Framework Core延遲加載(懶加載)用法的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Entity Framework Core使用控制臺程序生成數(shù)據(jù)庫表
- Entity?Framework?Core實(shí)現(xiàn)Like查詢詳解
- Entity Framework Core中執(zhí)行SQL語句和存儲過程的方法介紹
- Entity Framework Core批處理SQL語句
- Entity Framework Core實(shí)現(xiàn)軟刪除與查詢過濾器
- Entity Framework Core生成列并跟蹤列記錄
- ASP.NET Core在WebApi項(xiàng)目中使用MiniProfiler分析Entity Framework Core
- Entity Framework Core工具使用命令行
- Entity?Framework?Core關(guān)聯(lián)刪除
- 詳解如何在ASP.NET Core中應(yīng)用Entity Framework
- Entity Framework Core對Web項(xiàng)目生成數(shù)據(jù)庫表
相關(guān)文章
Web API身份認(rèn)證解決方案之Basic基礎(chǔ)認(rèn)證
本文詳細(xì)講解了Web API身份認(rèn)證解決方案之Basic基礎(chǔ)認(rèn)證,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03ASP.NET MVC 4使用PagedList.Mvc分頁的實(shí)現(xiàn)代碼
本篇文章主要介紹了ASP.NET MVC 4使用PagedList.Mvc分頁的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07ASP.NET MVC5+EF6+EasyUI后臺管理系統(tǒng) 微信公眾平臺開發(fā)之消息管理
這篇文章主要介紹了ASP.NET MVC5+EF6+EasyUI后臺管理系統(tǒng),微信公眾平臺開發(fā)之消息管理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09ASP.NET設(shè)計(jì)網(wǎng)絡(luò)硬盤之上傳文件實(shí)現(xiàn)代碼
用戶最終是要和文件打交道的,文件夾僅僅是用來方便管理的。文件的上傳和下載也就成為“網(wǎng)絡(luò)硬盤”功能設(shè)計(jì)中的重要一環(huán)2012-10-10詳解ASP.NET Core和ASP.NET Framework共享身份驗(yàn)證
本篇文章主要介紹了詳解ASP.NET Core和ASP.NET Framework共享身份驗(yàn)證 ,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12