日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Entity?Framework主從表數據加載方式_C#教程

作者:springsnow ? 更新時間: 2022-08-05 編程語言

一、延遲加載:LazyLoading

使用延遲加載,關聯的實體必須標注為virtual。

本例是標注Destination類里的Lodgings為virtual。因為先發sql去查詢主鍵對象,然后根據主鍵id去從表里查相關聯的數據。

    private static void TestLazyLoading()
    {
        using (var context = new CodeFirst.DataAccess.BreakAwayContext())
        {
           var canyon = (from d in context.Destinations where d.Name == "Grand Canyon" select d).Single();

           var distanceQuery = from l in canyon.Lodgings   //延遲加載canyon的所有.Lodgings
                                where l.Name == "HuangShan Hotel"
                                select l;
            foreach (var lodging in distanceQuery)
                Console.WriteLine(lodging.Name);
        }
    }

改進:在數據庫中操作,顯示加載

    private static void QueryLodgingDistancePro()
    {
        using (var context = new CodeFirst.DataAccess.BreakAwayContext())
        {
            var canyon = (from d in context.Destinations where d.Name == "Grand Canyon" select d).Single();
            var lodgingQuery = context.Entry(canyon).Collection(d => d.Lodgings).Query();//接下來的查詢在數據庫中,包括Count()等
            var distanceQuery = from l in lodgingQuery 
                                 where l.Name == "HuangShan Hotel" 
                                 select l;

            foreach (var lodging in distanceQuery)
                Console.WriteLine(lodging.Name);
        }
    }

二、貪婪加載:EagerLoading

    private static void TestEagerLoading()
    {
        using (var context = new CodeFirst.DataAccess.BreakAwayContext())
        {
            // var allDestinations = context.Destinations.Include(d => d.Lodgings);
            var AustraliaDestination = context.Destinations.Include(d => d.Lodgings).Where(d => d.Name == "Bali");
            //context.Lodgings.Include(l => l.PrimaryContact.Photo);
            //context.Destinations.Include(d => d.Lodgings.Select(l => l.PrimaryContact));
            //context.Lodgings.Include(l => l.PrimaryContact).Include(l => l.SecondaryContact);
            foreach (var destination in AustraliaDestination)
            {
                foreach (var lodging in destination.Lodgings)
                    Console.WriteLine(" - " + lodging.Name);
            }
        }
    }

三、顯示加載:ExplicitLoading

1、查找導航屬性為一個集合的

    private static void LoadRelateData()
    {
        using (var context = new CodeFirst.DataAccess.BreakAwayContext())
        {
           var canyon = (from d in context.Destinations where d.Name == "Grand Canyon" select d).Single();

           context.Entry(canyon).Collection(d => d.Lodgings).Load();  //顯示加載

           foreach (var lodging in context.Lodgings.Local)
                Console.WriteLine(lodging.Name);
        }
    }

2、查找導航屬性為一個實體對象的

    private static void LoadPrimaryKeyData()
    {
        using (var context = new CodeFirst.DataAccess.BreakAwayContext())
        {
            var lodging = context.Lodgings.First();

            context.Entry(lodging).Reference(l => l.Destination).Load();

           foreach (var destination in context.Destinations.Local)   //遍歷的是內存中的Destinations數據
                Console.WriteLine(destination.Name);
        }
    }

原文鏈接:https://www.cnblogs.com/springsnow/p/13251419.html

欄目分類
最近更新