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

    Lazy Loading:

    One of the important functions of Entity Framework is lazy loading. Lazy loading means delaying the loading of related data, until you specifically request for it. For example, Student class contains StudentAddress as a complex property. So, the context first loads all the students from the database, then it will load the address of a particular student when we access StudentAddress property as shown below.

    using (var ctx = new SchoolDBEntities())
    {
            //Loading students only
            IList<Student> studList = ctx.Students.ToList<Student>();
    
            Student std = studList[0];
    
            //Loads Student address for particular Student only (seperate SQL query)
            StudentAddress add = std.StudentAddress;
    }

    The code shown above will result in two SQL queries. First, it will fetch all students:

    SELECT 
    [Extent1].[StudentID] AS [StudentID], 
    [Extent1].[StudentName] AS [StudentName], 
    [Extent1].[StandardId] AS [StandardId]
    FROM [dbo].[Student] AS [Extent1]

    The, it will send the following query when we get the reference of StudentAddress:

    exec sp_executesql N'SELECT 
    [Extent1].[StudentID] AS [StudentID], 
    [Extent1].[Address1] AS [Address1], 
    [Extent1].[Address2] AS [Address2], 
    [Extent1].[City] AS [City], 
    [Extent1].[State] AS [State]
    FROM [dbo].[StudentAddress] AS [Extent1]
    WHERE [Extent1].[StudentID] = @EntityKeyValue1',N'@EntityKeyValue1 int',@EntityKeyValue1=1

    However, you can also turn off lazy loading for a particular property or an entire context. To turn off lazy loading for a particular property, do not make it virtual. To turn off lazy loading for all entities in the context, set its configuration property to false:

    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.Core.Objects;
    using System.Linq;
        
    public partial class SchoolDBEntities : DbContext
    {
        public SchoolDBEntities(): base("name=SchoolDBEntities")
        {
            this.Configuration.LazyLoadingEnabled = false;
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    }

    Rules for lazy loading:

    1. context.Configuration.ProxyCreationEnabled should be true.
    2. context.Configuration.LazyLoadingEnabled should be true.
    3. Navigation property should be defined as public, virtual. Context will NOT do lazy loading if the property is not defined as virtual.

    Learn how to load entities explicitly in the next section.

  • 相关阅读:
    2019.4.1 JMeter中文乱码解决方案
    19.3.25 sql查询语句
    2019.3.23 python的unittest框架与requests
    2019.3.22 JMeter基础操作
    19.3.21 计算机网络基础知识
    19.3.20 cmd操作:1.dir查看当前文件夹内的文件;2.alt+space+c关闭cmd窗口
    19.3.20 解决pycharm快捷键无法使用问题和熟悉git与码云操作流程
    19.3.19 使用Flask框架搭建一个简易登录服务器
    回调函数
    var img = new Image()
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5649299.html
Copyright © 2011-2022 走看看