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);
}