【发布时间】:2017-09-24 12:33:54
【问题描述】:
我使用以下代码禁用了 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);
}
【问题讨论】:
-
EF:核心还是非核心?版本?
-
实体框架6.1
-
听起来很奇怪,给我们看一个minimal reproducible example,和/或生成的sql。
-
还有,哪个提供商?
-
SqlClient 提供者
标签: c# entity-framework lazy-loading