zoukankan      html  css  js  c++  java
  • Entity Framework Tutorial Basics(36):Eager Loading

    Eager Loading:

    Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved using the Include() method.

    In the following example, it gets all the students from the database along with it's standards using Include() method.

    LINQ Query Syntax:

    using (var context = new SchoolDBEntities())
    {
           var res = (from s in context.Students.Include("Standard")
                            where s.StudentName == "Student1"
                            select s).FirstOrDefault<Student>();
    }

    LINQ Method Syntax:

    using (var ctx = new SchoolDBEntities())
    {
           stud = ctx.Students.Include("Standard")
                                   .Where(s => s.StudentName == "Student1").FirstOrDefault<Student>();
               
    }

    The code shown above will result in following SQL query:

    SELECT TOP (1) 
    [Extent1].[StudentID] AS [StudentID], 
    [Extent1].[StudentName] AS [StudentName], 
    [Extent2].[StandardId] AS [StandardId], 
    [Extent2].[StandardName] AS [StandardName], 
    [Extent2].[Description] AS [Description]
    FROM  [dbo].[Student] AS [Extent1]
    LEFT OUTER JOIN [dbo].[Standard] AS [Extent2] ON [Extent1].[StandardId] = [Extent2].[StandardId]
    WHERE 'Student1' = [Extent1].[StudentName]

    Use Lambda Expression:

    You can also use linq lambda expression in Include method. For this, take a reference ofSystem.Data.Entity namespace and use lambda expression as shown below.

    using System;
    using System.Data.Entity; 
       
    class Program
    {
        static void Main(string[] args)
        {
        
            using (var ctx = new SchoolDBEntities())
            {
                stud = ctx.Students.Include(s => s.Standard)
                                    .Where(s => s.StudentName == "Student1")
                                    .FirstOrDefault<Student>();
               
            }
        }
    }

    The code shown above will result in the following SQL query:

    SELECT TOP (1) 
    [Extent1].[StudentID] AS [StudentID], 
    [Extent1].[StudentName] AS [StudentName], 
    [Extent2].[StandardId] AS [StandardId], 
    [Extent2].[StandardName] AS [StandardName], 
    [Extent2].[Description] AS [Description]
    FROM  [dbo].[Student] AS [Extent1]
    LEFT OUTER JOIN [dbo].[Standard] AS [Extent2] ON [Extent1].[StandardId] = [Extent2].[StandardId]
    WHERE 'Student1' = [Extent1].[StudentName]

    Load multiple levels of related entities:

    You can also eagerly load multiple levels of related entities. The code snippet shown below loads related Student, Standard and Teachers:

    using (var ctx = new SchoolDBEntities())
    {
            stud = ctx.Students.Include("Standard.Teachers")
                                .Where(s => s.StudentName == "Student1")
                                .FirstOrDefault<Student>();
    }

    Or using lambda expression as below.

    using (var ctx = new SchoolDBEntities())
    {
            stud = ctx.Students.Include(s => s.Standard.Teachers)
                                .Where(s => s.StudentName == "Student1")
                                .FirstOrDefault<Student>();
    }

    The code shown above results in the following SQL query:

    SELECT [Project2].[StudentID] AS [StudentID], 
    [Project2].[StudentName] AS [StudentName], 
    [Project2].[StandardId] AS [StandardId], 
    [Project2].[StandardName] AS [StandardName], 
    [Project2].[Description] AS [Description], 
    [Project2].[C1] AS [C1], 
    [Project2].[TeacherId] AS [TeacherId], 
    [Project2].[TeacherName] AS [TeacherName], 
    [Project2].[StandardId1] AS [StandardId1]
    FROM ( SELECT 
        [Limit1].[StudentID] AS [StudentID], 
        [Limit1].[StudentName] AS [StudentName], 
        [Limit1].[StandardId1] AS [StandardId], 
        [Limit1].[StandardName] AS [StandardName], 
        [Limit1].[Description] AS [Description], 
        [Project1].[TeacherId] AS [TeacherId], 
        [Project1].[TeacherName] AS [TeacherName], 
        [Project1].[StandardId] AS [StandardId1], 
        CASE WHEN ([Project1].[TeacherId] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
        FROM   (SELECT TOP (1) [Extent1].[StudentID] AS [StudentID], [Extent1].[StudentName] AS [StudentName], [Extent1].[StandardId] AS [StandardId2], [Extent2].[StandardId] AS [StandardId1], [Extent2].[StandardName] AS [StandardName], [Extent2].[Description] AS [Description]
            FROM  [dbo].[Student] AS [Extent1]
            LEFT OUTER JOIN [dbo].[Standard] AS [Extent2] ON [Extent1].[StandardId] = [Extent2].[StandardId]
            WHERE 'updated student' = [Extent1].[StudentName] ) AS [Limit1]
        LEFT OUTER JOIN  (SELECT 
            [Extent3].[TeacherId] AS [TeacherId], 
            [Extent3].[TeacherName] AS [TeacherName], 
            [Extent3].[StandardId] AS [StandardId]
            FROM [dbo].[Teacher] AS [Extent3]
            WHERE [Extent3].[StandardId] IS NOT NULL ) AS [Project1] ON [Limit1].[StandardId2] = [Project1].[StandardId]
    )  AS [Project2]
    ORDER BY [Project2].[StudentID] ASC, [Project2].[StandardId] ASC, [Project2].[C1] ASC

    Learn how Entity Framework supports lazy loading of entities, in the next section.

  • 相关阅读:
    深入理解jvm分享培训pdf(转载) 老李
    innobackupex自动备份脚本(增量备份,自动压缩)
    多线程调用生成主键流水号存储过程产生主键冲突问题解决方案
    mysql 5.7新数据库sys解析(一)
    根据日期累加金额的mysql
    mysql字符串分割函数(行转列)
    使用innobackupex备份mysql数据库
    css学习inlineblock详解及dispaly:inline inlineblock block 三者区别精要概括
    html良好结构之豆瓣风格
    HTML5学习笔记html5与传统html区别
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5649281.html
Copyright © 2011-2022 走看看