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

Entity Framework主從表數(shù)據(jù)加載方式

 更新時(shí)間:2022年06月13日 14:38:13   作者:springsnow  
這篇文章介紹了Entity Framework主從表數(shù)據(jù)加載方式,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、延遲加載: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);
        }
    }

到此這篇關(guān)于Entity Framework主從表數(shù)據(jù)加載方式的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#判斷當(dāng)前程序是否通過管理員運(yùn)行的方法

    C#判斷當(dāng)前程序是否通過管理員運(yùn)行的方法

    這篇文章主要介紹了C#判斷當(dāng)前程序是否通過管理員運(yùn)行的方法,可通過非常簡(jiǎn)單的系統(tǒng)函數(shù)調(diào)用實(shí)現(xiàn)對(duì)當(dāng)前程序是否通過管理員運(yùn)行進(jìn)行判定,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-11-11
  • Unity?UGUI?按鈕綁定事件的?4?種方式匯總

    Unity?UGUI?按鈕綁定事件的?4?種方式匯總

    UGUI?可視化創(chuàng)建以及關(guān)聯(lián)事件很方便,?動(dòng)態(tài)創(chuàng)建可以利用創(chuàng)建好的?Prefab?進(jìn)行實(shí)例化,?只是在關(guān)聯(lián)事件上有些復(fù)雜,這篇文章主要介紹了Unity?UGUI?按鈕綁定事件的?4?種方式,需要的朋友可以參考下
    2022-01-01
  • C# javascript 讀寫Cookie的方法

    C# javascript 讀寫Cookie的方法

    這篇文章介紹了C# javascript 讀寫Cookie的方法,有需要的朋友可以參考一下
    2013-10-10
  • 積累Visual Studio 常用快捷鍵的動(dòng)畫演示

    積累Visual Studio 常用快捷鍵的動(dòng)畫演示

    在代碼開發(fā)過程中,頻繁的使用鍵盤、鼠標(biāo)操作非常麻煩,影響程序的開發(fā)效率。如何操作能用鍵盤來操作,那就節(jié)省時(shí)間了。下面小編把我平時(shí)積累的有關(guān)visul studio 常用快捷鍵的動(dòng)畫演示分享給大家,僅供大家參考
    2015-10-10
  • C#中矩陣運(yùn)算方法實(shí)例分析

    C#中矩陣運(yùn)算方法實(shí)例分析

    這篇文章主要介紹了C#中矩陣運(yùn)算方法,實(shí)例分析了通過C#實(shí)現(xiàn)矩陣的初始化、轉(zhuǎn)置矩陣、求逆矩陣等各種常用的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • WinForm之BindingSource基礎(chǔ)操作實(shí)例教程

    WinForm之BindingSource基礎(chǔ)操作實(shí)例教程

    這篇文章主要介紹了WinForm之BindingSource基礎(chǔ)操作,對(duì)BindingSource組建的用法進(jìn)行較為深入的實(shí)例分析,需要的朋友可以參考下
    2014-08-08
  • C#多線程的Join()方法

    C#多線程的Join()方法

    這篇文章介紹了C#多線程的Join()方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#中加載dll并調(diào)用其函數(shù)的實(shí)現(xiàn)方法

    C#中加載dll并調(diào)用其函數(shù)的實(shí)現(xiàn)方法

    下面小編就為大家?guī)硪黄狢#中加載dll并調(diào)用其函數(shù)的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • C#使用semaphore來管理異步下載請(qǐng)求的方法

    C#使用semaphore來管理異步下載請(qǐng)求的方法

    這篇文章主要介紹了C#使用semaphore來管理異步下載請(qǐng)求的方法,涉及C#使用semaphore實(shí)現(xiàn)多線程管理的技巧,需要的朋友可以參考下
    2015-06-06
  • C# MVC 使用LayUI實(shí)現(xiàn)下拉框二級(jí)聯(lián)動(dòng)的功能

    C# MVC 使用LayUI實(shí)現(xiàn)下拉框二級(jí)聯(lián)動(dòng)的功能

    這篇文章主要介紹了C# MVC 如何使用LayUI實(shí)現(xiàn)下拉框二級(jí)聯(lián)動(dòng),文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06

最新評(píng)論