欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Entity Framework Core延遲加載(懶加載)用法

 更新時(shí)間:2022年02月22日 16:24:47   作者:Sweet-Tang  
這篇文章介紹了Entity Framework Core延遲加載(懶加載)的使用方式,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

眾所周知在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)用程序中使用,只需在DbContextOnConfiguring方法中添加對UseLazyLoadingProxies()擴(kuò)展方法調(diào)用即可。

框架目前是通過Castle.Core框架來生成代理類來實(shí)現(xiàn)對導(dǎo)航屬性的延遲加載,開發(fā)團(tuán)隊(duì)打算將該功能做為EF Core的可選安裝包。

到此這篇關(guān)于Entity Framework Core延遲加載(懶加載)用法的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論