Entity framework EF5代码优先:新创建的实体';虽然使用了代理,但相关实体不会延迟加载

Entity framework EF5代码优先:新创建的实体';虽然使用了代理,但相关实体不会延迟加载,entity-framework,lazy-loading,code-first,poco,Entity Framework,Lazy Loading,Code First,Poco,考虑以下代码段: // this.ctx is an instance of our EF Code First DB Context. // the entity in this example is called "Something", and the DB set "Somethings". // there is also another entity type called "SomethingElse". // the Something entity is declared li

考虑以下代码段:

// this.ctx is an instance of our EF Code First DB Context.
// the entity in this example is called "Something", and the DB set "Somethings".
// there is also another entity type called "SomethingElse".
// the Something entity is declared like this:
// 
// public class Something {
//   public int Foo { get; set; }
//   public string Bar { get; set; }
// 
//   public virtual IList<SomethingElse> RelatedStuff { get; set; }
// }
// 

// Create is used to ensure a proxy is created.    
var something = this.ctx.Somethings.Create();

// The new entity is added
this.ctx.Somethings.Add(something);    

// lazy loading: ON
System.Diagnostics.Debug.Assert(this.ctx.Configuration.LazyLoadingEnabled);    

// the entity is really in "added" state
System.Diagnostics.Debug.Assert(this.ctx.Entry(something).State == EntityState.Added);

// *** lazy loading does not work! ***
System.Diagnostics.Debug.Assert(something.RelatedStuff == null);

// note: if stepping through this with the debugger, I can confirm that "something" is
//       of the DynamicProxy type, not a plain "Something" POCO.

// but, if we change the state manually...
this.ctx.Entry(something).State = EntityState.Unchanged;

// *** it works!! ***    (doing a this.ctx.SaveChanges(), actually, also makes this work)
System.Diagnostics.Debug.Assert(something.RelatedStuff!= null);
//this.ctx是EF code First DB上下文的一个实例。
//本例中的实体称为“Something”,DB集称为“Something”。
//还有另一种实体类型,称为“SomethingElse”。
//实体声明如下:
// 
//公开课{
//公共int Foo{get;set;}
//公共字符串条{get;set;}
// 
//公共虚拟IList RelatedStuff{get;set;}
// }
// 
//创建用于确保创建代理。
var something=this.ctx.something.Create();
//将添加新实体
this.ctx.Somethings.Add(某物);
//延迟加载:ON
System.Diagnostics.Debug.Assert(this.ctx.Configuration.LazyLoadingEnabled);
//实体实际上处于“添加”状态
System.Diagnostics.Debug.Assert(this.ctx.Entry(something.State==EntityState.Added);
//***延迟加载不起作用***
System.Diagnostics.Debug.Assert(something.RelatedStuff==null);
//注意:如果使用调试器单步执行此操作,我可以确认“something”是正确的
//属于DynamicProxy类型,而不是普通的“某物”POCO。
//但是,如果我们手动更改状态。。。
this.ctx.Entry(something).State=EntityState.Unchanged;
//***有效!!***(实际上,执行this.ctx.SaveChanges()操作也可以实现此功能)
System.Diagnostics.Debug.Assert(something.RelatedStuff!=null);
有人能向我解释一下为什么新创建的POCO上的延迟加载不起作用,尽管延迟加载已启用,并且该属性是虚拟的,并且在更改状态时,它会神奇地开始工作?如果我没有弄错的话,即使对于瞬态对象,延迟加载也应该可以工作,不是吗

干杯,
Tim

在entity framework 5中,似乎默认情况下禁用了延迟加载功能, 我在web上没有发现任何有用的东西,只是在定义了“DbContext”对象之后,在代码中明确地设置了此功能,从而解决了我的问题:

protected DbContext Context;
protected IDbSet<T> DbSet;

public Repository(DbContext context)
{
     Context = context;
     DbSet = Context.Set<T>();

     Context.Configuration.LazyLoadingEnabled = true;
}
受保护的DbContext上下文;
受保护的IDbSet-DbSet;
公共存储库(DbContext上下文)
{
上下文=上下文;
DbSet=Context.Set();
Context.Configuration.LazyLoadingEnabled=true;
}

我希望这对你有帮助

正如我在原始问题中提到的,在我的测试用例中确实启用了延迟加载。