1

I have disabled lazy loading of EF 6.1 using the following code

public MyContext() : base("DefaultConnection")
{
    this.Configuration.LazyLoadingEnabled = false;
    this.Configuration.ProxyCreationEnabled = false;
}

Then I load my object using the following line.

T result = (T)context.Set<T>().Find(id);

Where T is an object in my domain that has some navigation properties. I am expecting this Find method to return the object without the navigation properties since I have disabled lazy loading, but when I run my code and check the variable values I find that the navigation properties were loaded too! Anybody knows what the problem might be?

EDIT

Here is a mini sample

MyContext

public class MyContext : DbContext
{

    public MyContext() : base("DefaultConnection")
    {
        this.Configuration.LazyLoadingEnabled = false;
        this.Configuration.ProxyCreationEnabled = false;
    }

    public DbSet<Lesson> Lessons { get; set; }
    public DbSet<Part> Parts { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }

}

Model

public class Lesson
{

    public int Id { get; set; }
    public Part Part { get; set; }

}


public class Part
{

    public int Id { get; set; }

    public string Name { get; set; }
}

Client Code

            using (MyContext c = new EFTest.MyContext())
            {

                Lesson d = new EFTest.Lesson();
                d.Part = new EFTest.Part() { Name = "a" };

                Lessson insert = c.Lessons.Add(d);
                c.SaveChanges();

                Lesson returned = c.Lessons.Find(insert.Id);
            }
Sisyphus
  • 900
  • 12
  • 32

1 Answers1

2

It turned out that the problem was with my client code. When I try to find an object I have just inserted EF gets it from its cache where it already exists with the full graph, so the full graph is returned. But when I tried to Find(1) instead of Find(Insert.Id) it returned a shallow object correctly. Also using the AsNoTracking method on the DbSet yielded the same result.

Sisyphus
  • 900
  • 12
  • 32