LazyLoadingEnabled setting doesn't seem to work in EF 5

I'm using EF Model first with POCO entities and with custom DbContexts. My problem is that setting LazyLoadingEnabled=false does not affect anything, navigation properties are still loaded. Below is my example simplified.

The entity Program. A program can be part of other programs:

namespace Domain.Entities
    using System;
    using System.Collections.Generic;

    public partial class Program
        public Program()
            this.Programs = new HashSet<Program>();

        public int Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public System.DateTime StartDate { get; set; }
        public System.DateTime EndDate { get; set; }
        public Nullable<int> ProgramId { get; set; }

        public virtual ICollection<Program> Programs { get; set; }
        public virtual Program OwnerProgram { get; set; }

The DbContext:

namespace Infrastructure.Model
    public class ProgramContext : DbContext
        public ProgramContext()
            : base("name=MyContainer")
            Configuration.LazyLoadingEnabled = false;

        public DbSet<Program> Programs { get; set; }

Here is how I use it:

private ProgramContext _dbContext = new ProgramContext();

// GET api/program
public IEnumerable<Program> GetPrograms()
    List<Program> list = _dbContext.Programs.ToList();
    return list;

With the above sample, EF still loads the Programs and OwnerProgram properties of the Program class. I have tried removing the virtual keywords, disabling the proxy creation and also verified that LazyLoadingEnabled=false on the Model itself.

Am I missing something?


The effect you are seeing is called relationship fixup.

Actually the navigation properties are not loaded explicitly. The query _dbContext.Programs.ToList() only loads the whole Programs table from the database. This is just a simple SQL query (like SELECT * FROM ProgramsTable) without any WHERE clause and without any JOIN to related rows.

Also no lazy loading happens here (it really can't if you disable it and if you disable even dynamic proxies) when you access the program.Programs and program.OwnerProgram navigation properties.

The navigation properties get populated when the result from your query is materialized because your query (that loads all programs) will have loaded all programs that the navigation properties can refer to. EF detects that those related entities are already in memory and put them into the navigation properties automatically.

You can verify this if you don't load all programs but only, for example, a single one:

Program program  = _dbContext.Programs.FirstOrDefault();

Now, program.Programs and program.OwnerProgram will be null - unless the loaded program is part of its own program.OwnerProgram collection or is its own OwnerProgram.

Need Your Help

ggplot to stack bar graph top 5 for each month

r ggplot2

I have a good one. I've thinking about this a long time now. I have this data set and this data set could be be huge. I would like to graph a ggplot stack bar based on top 5 higest count for each m...

Adjusting MKMapView's Frame on iOS 6 in Objective-C

objective-c ios6 mkmapview

I'm trying to adjust the frame of my MKMapView created on IB for 4-inch devices but it doesn't work. Here's my code on viewDidLoad:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.