Entity Framework Core 懒加载
眾所周知在EF 6 及以前的版本中,是支持懶加載(Lazy Loading)的,可惜在EF Core 并不支持,必須使用Include方法來(lái)支持導(dǎo)航屬性的數(shù)據(jù)加載。不過(guò)現(xiàn)在EF Core的開(kāi)發(fā)團(tuán)隊(duì)打算恢復(fù)對(duì)這一功能的支持(目前還未發(fā)布,不過(guò)可以在Github上面下載進(jìn)行測(cè)試)。
懶加載
懶加載也可以叫做按需加載、延遲加載。可以分兩方面來(lái)理解,一方面指暫時(shí)不需要該數(shù)據(jù),不用在當(dāng)前馬上加載,而可以推遲到使用它時(shí)再加載;另一方面指不確定是否將會(huì)需要該數(shù)據(jù),所以暫時(shí)請(qǐng)不要加載,待確定需要后再加載它。懶加載是一種很重要的數(shù)據(jù)訪(fǎng)問(wèn)特性,可以有效地減少與數(shù)據(jù)源的交互(注意,這里所提的交互不是指交互次數(shù),而是指交互的數(shù)據(jù)量),從而提升程序性能。
EF 6 懶加載
我們先來(lái)看一看在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; }}
我們?cè)谶@里定義訂單、訂單明細(xì)實(shí)體,它們是一對(duì)多關(guān)系,通過(guò)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);}}在查詢(xún)訂單號(hào)為 10253 的訂單后,如果我們需要訪(fǎng)問(wèn)訂單的明細(xì),不需要再編寫(xiě)一次數(shù)據(jù)查詢(xún)的代碼,直接訪(fǎng)問(wèn)導(dǎo)航屬性即可,EF會(huì)自動(dòng)幫我們填充屬性的值。
懶加載需要注意以下兩點(diǎn):
在配置中啟用了懶加載(默認(rèn)開(kāi)啟);
實(shí)體類(lèi)不能是封閉(sealed)類(lèi),導(dǎo)航屬性必須是虛(virtual)屬性。
在 EF Core 中啟用懶加載
目前EF Core發(fā)布的最新版本中并不支持懶加載,開(kāi)發(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上面下載最新的源代碼來(lái)測(cè)試這一功能?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方法中添加對(duì)UseLazyLoadingProxies()擴(kuò)展方法調(diào)用即可。
框架目前是通過(guò)Castle.Core框架來(lái)生成代理類(lèi)來(lái)實(shí)現(xiàn)對(duì)導(dǎo)航屬性的延遲加載,開(kāi)發(fā)團(tuán)隊(duì)打算將該功能做為EF Core的可選安裝包。
如果您對(duì)該功能感興趣,可以在Github上面下載源代碼進(jìn)行測(cè)試。
原文地址:https://www.cnblogs.com/tdfblog/p/entity-framework-lazy-loading.html?
.NET社區(qū)新聞,深度好文,歡迎訪(fǎng)問(wèn)公眾號(hào)文章匯總 http://www.csharpkit.com
總結(jié)
以上是生活随笔為你收集整理的Entity Framework Core 懒加载的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Actor-ES框架:Ray
- 下一篇: Actor-ES框架:Ray--事件(E