C# 延迟加载的实体框架6抛出ObjectDisposedException

C# 延迟加载的实体框架6抛出ObjectDisposedException,c#,entity-framework,linq-to-entities,lazy-loading,ef-database-first,C#,Entity Framework,Linq To Entities,Lazy Loading,Ef Database First,我正在使用EF6,首先是延迟加载和数据库 我在实体帖子中有以下导航属性: 帖子、评论 Posts.comments参考 员额.类别 这两个代码: 代码1 这很好,我可以导航到类别 代码2 当我尝试导航到注释时,会抛出一个ObjectDisposedException 为什么??是因为加入 编辑:在@user2674389的代码建议下结束时,我切换到LINQ to Entities,它运行良好: var query = from c in Context.Posts.Include(p =&g

我正在使用EF6,首先是延迟加载和数据库

我在实体帖子中有以下导航属性:

  • 帖子、评论
  • Posts.comments参考
  • 员额.类别
这两个代码:

代码1 这很好,我可以导航到类别

代码2 当我尝试导航到注释时,会抛出一个ObjectDisposedException

为什么??是因为加入


编辑:在@user2674389的代码建议下

结束时,我切换到LINQ to Entities,它运行良好:

var query = from c in Context.Posts.Include(p => p.Comments)
        join h in Context.Users on c.WritterID equals h.UserID
        where h.Users.CompareTo("foo user") == 0
        select c;

但我仍然想知道如何在lambda表达式中执行此操作…

此外,建议在Include:
中使用表达式作为选择器。Include(p=>p.Comment)
。这样,当(出于任何原因)注释导航属性被重命名或删除,而您忘记调整include时,您将得到编译错误而不是运行时错误。@user2674389 sintaxt不允许我使用
。include(p=>p.Comment)
,仅
。include(System.String)
。我可以需要一个新的推荐人吗?关于投影:如您所见,我不需要用户数据,因此如何在最终投影中包含注释?对于
include
的表达式版本,您需要添加
System.data.Entity
命名空间。
var query = Context.Posts.Include(p => p.Comments)
   .Join(Context.Users,
   t => t.WritterID,
   h => h.UserID,
   (t, h) => new { Posts= t, Users= h })
   .Where(q => q.Users.Name == "foo user")
   .Select(x => x.Posts)
   .ToList()
var query = from c in Context.Posts.Include(p => p.Comments)
        join h in Context.Users on c.WritterID equals h.UserID
        where h.Users.CompareTo("foo user") == 0
        select c;