我使用以下代码禁用了EF 6.1的延迟加载
public MyContext() : base("DefaultConnection")
{
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
}
然后我使用下面的行加载我的对象。
T result = (T)context.Set<T>().Find(id);
其中T是我的域中具有一些导航属性的对象。我期待着这个
Find
方法返回不带导航属性的对象,因为我已禁用延迟加载,但当我运行代码并检查变量值时,我发现导航属性也已加载!有人知道问题出在哪里吗?
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);
}
}
模型
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; }
}
客户端代码
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);
}