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.

原文地址:https://www.cnblogs.com/malaikuangren/p/2604221.html