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

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Entity?Framework主從表數(shù)據(jù)加載方式_C#教程

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

一、延遲加載:LazyLoading

使用延遲加載,關(guān)聯(lián)的實(shí)體必須標(biāo)注為virtual。

本例是標(biāo)注Destination類里的Lodgings為virtual。因?yàn)橄劝l(fā)sql去查詢主鍵對(duì)象,然后根據(jù)主鍵id去從表里查相關(guān)聯(lián)的數(shù)據(jù)。

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

改進(jìn):在數(shù)據(jù)庫中操作,顯示加載

    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();//接下來的查詢?cè)跀?shù)據(jù)庫中,包括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、查找導(dǎo)航屬性為一個(gè)集合的

    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、查找導(dǎo)航屬性為一個(gè)實(shí)體對(duì)象的

    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)   //遍歷的是內(nèi)存中的Destinations數(shù)據(jù)
                Console.WriteLine(destination.Name);
        }
    }

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

欄目分類
最近更新