zoukankan      html  css  js  c++  java
  • Understanding Lazy Loading in EF 4

    Entities Encapsulate 1-to-many relationships

    But when is the data loaded – And what does “Lazy” Mean?

      • Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed.
      • It can contribute to efficiency in the program's operation if properly and appropriately used. 
        The opposite of lazy loading is Eager Loading.

     

    Like the debate for FK Association, many customers persist that Lazy Loading should be a necessity for an ORM like EF, while others believe it is evil. Also in EFv1, Lazy Loading is not available. Fortunately, thanks to EF team’s effort again, Lazy Loading becomes the default option in EF4, as we already have in LINQ to SQL. Of course, we can still use Eager Loading and Explicit Loading as our wish. Let’s see how they work in EF4.

    Supposed we have the db model like this.

    public class School
        {
            public School()
            {
                this.ClassRooms = new List<ClassRoom>();
            }
    
            public int Id { get; set; }
            public string Name { get; set; }
            public string Address { get; set; }
            public virtual ICollection<ClassRoom> ClassRooms { get; set; }
        }
    
    public class ClassRoom
        {
            public ClassRoom()
            {
                this.Students = new List<Student>();
            }
    
            public int Id { get; set; }
            public string Name { get; set; }
            public int SchoolId { get; set; }
            public virtual School School { get; set; }
            public virtual ICollection<Student> Students { get; set; }
        }
    
    public class Student
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int ClassRoomId { get; set; }
            public virtual ClassRoom ClassRoom { get; set; }
        }

    the db schema is below

    I am using the power tool for ef to generate the code above.

    Supposed we have some test code looks like below.

    private static void Test()
            {
    
                using (var ctx = new testOneToManySchContextContext())
                {
    
                    //ctx.Configuration.LazyLoadingEnabled = true;
    
                    School c = ctx.Schools.First();
                    Console.WriteLine("count is " + c.ClassRooms.Count());
    
                    Console.ReadLine();
                }
            }

    just now . we use the default value of LazyloadingEnabled . which is true;

    and we will see what 's going on with Lazyloading in the sql tracing.

    when the related obj of school . which is classroom . will be loaded from query when it is needed . what means "needed"? in this case . we need to count the number of classroom of the first school .

    if we disabled the lazy loading . we will found the count is 0.

    unless we can include the navigation property . 

    like below:

     private static void Test()
            {
    
                using (var ctx = new testOneToManySchContextContext())
                {
    
                    ctx.Configuration.LazyLoadingEnabled = false;
    
                    School c = ctx.Schools.Include("ClassRooms").First();
                    Console.WriteLine("count is " + c.ClassRooms.Count());
    
                    Console.ReadLine();
                }
            }

    then we will a left out join query in the sql trace. whether or not you enabled the lazyloading.

  • 相关阅读:
    PAIP.img ROM文件提取APK
    paip.提升程序稳定性最佳实践
    paip.验证码识别序列号的反转
    paip.android APK安装方法大总结系统应用的安装
    paip.php调试脱离IDE VC59
    paip.声音按键音延迟的解决
    paip.提升效率几款任务栏软件vc59
    paip.android 读取docx总结
    paip.C#.NET多线程访问 toolStripStatusLabel VC421
    paip.Image对象出现“对象当前正在其他地方使用或者GDI+中发生一般性错误的解决
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2604221.html
Copyright © 2011-2022 走看看