日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Entity Framework Core 懒加载

發布時間:2023/12/4 68 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Entity Framework Core 懒加载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

眾所周知在EF 6 及以前的版本中,是支持懶加載(Lazy Loading)的,可惜在EF Core 并不支持,必須使用Include方法來支持導航屬性的數據加載。不過現在EF Core的開發團隊打算恢復對這一功能的支持(目前還未發布,不過可以在Github上面下載進行測試)。

懶加載

懶加載也可以叫做按需加載、延遲加載。可以分兩方面來理解,一方面指暫時不需要該數據,不用在當前馬上加載,而可以推遲到使用它時再加載;另一方面指不確定是否將會需要該數據,所以暫時請不要加載,待確定需要后再加載它。懶加載是一種很重要的數據訪問特性,可以有效地減少與數據源的交互(注意,這里所提的交互不是指交互次數,而是指交互的數據量),從而提升程序性能。

EF 6 懶加載

我們先來看一看在EF 6中的懶加載的使用方式。

實體定義:

? ?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; }}

我們在這里定義訂單訂單明細實體,它們是一對多關系,通過OrderId字段進行關聯。

? ? ? ?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 的訂單后,如果我們需要訪問訂單的明細,不需要再編寫一次數據查詢的代碼,直接訪問導航屬性即可,EF會自動幫我們填充屬性的值。

懶加載需要注意以下兩點:

  • 在配置中啟用了懶加載(默認開啟);

  • 實體類不能是封閉(sealed)類,導航屬性必須是虛(virtual)屬性。

在 EF Core 中啟用懶加載

目前EF Core發布的最新版本中并不支持懶加載,開發人員必須使用Include方法,才能完成導航屬性的加載。

? ? ? ?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);}}

要在通常的應用程序中使用,只需在DbContext的OnConfiguring方法中添加對UseLazyLoadingProxies()擴展方法調用即可。

框架目前是通過Castle.Core框架來生成代理類來實現對導航屬性的延遲加載,開發團隊打算將該功能做為EF Core的可選安裝包。

如果您對該功能感興趣,可以在Github上面下載源代碼進行測試。

原文地址:https://www.cnblogs.com/tdfblog/p/entity-framework-lazy-loading.html?


.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com

總結

以上是生活随笔為你收集整理的Entity Framework Core 懒加载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。