Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 从实体框架数据库优先移动到代码优先会给ObjectQuery带来问题,并且无法正确加载_Entity Framework_Orm_Ef Code First_Eager Loading_Ef Database First - Fatal编程技术网

Entity framework 从实体框架数据库优先移动到代码优先会给ObjectQuery带来问题,并且无法正确加载

Entity framework 从实体框架数据库优先移动到代码优先会给ObjectQuery带来问题,并且无法正确加载,entity-framework,orm,ef-code-first,eager-loading,ef-database-first,Entity Framework,Orm,Ef Code First,Eager Loading,Ef Database First,我们公司有一个使用 -实体框架4 -代码优先方法 该应用程序过去是基于实体框架数据库优先代码的,但是,我们将其更改为开始使用代码优先方法 我们有一些问题与Alex D James博客中提到的类似: 我们的旧代码基于实体框架数据库,第一个代码使用的代码如下: var results = ((from post in ctx.Posts from blog in post.Blogs where blog.Owner.EmailAddress

我们公司有一个使用

-实体框架4

-代码优先方法

该应用程序过去是基于实体框架数据库优先代码的,但是,我们将其更改为开始使用代码优先方法

我们有一些问题与Alex D James博客中提到的类似:

我们的旧代码基于实体框架数据库,第一个代码使用的代码如下:

 var results =
         ((from post in ctx.Posts
         from blog in post.Blogs
         where blog.Owner.EmailAddress == “alexj@microsoft.com”
         select post) as ObjectQuery<Post>).Include(“Comments”);
var results =
        (from post in ctx.Posts.Include(“Comments”)
        from blog in post.Blogs
        where blog.Owner.EmailAddress == “alexj@microsoft.com”
        select post);
当我们从实体框架数据库的第一个代码转移到第一个代码时,似乎出现了某种问题

我们将代码修改为如下所示:

 var results =
         ((from post in ctx.Posts
         from blog in post.Blogs
         where blog.Owner.EmailAddress == “alexj@microsoft.com”
         select post) as ObjectQuery<Post>).Include(“Comments”);
var results =
        (from post in ctx.Posts.Include(“Comments”)
        from blog in post.Blogs
        where blog.Owner.EmailAddress == “alexj@microsoft.com”
        select post);
但是,与评论相关的Post实体(即,评论集合的导航属性)将不会“立即加载”,这是有问题的,因为till将抛出以下错误:

Object reference not set to an instance of an object.
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
有人能告诉我如何修改代码,使其在实体框架代码优先方法中正常工作吗?

参考:

@ladislav mrnka说:

DbQuery包含Include方法,因此您不需要转换为ObjectQuery。ObjectQuery无法从DbQuery实例访问-它包装在内部类型InternalQuery中,并且未定义转换运算符

顺便说一句,当您使用System.Data.Entity和reference CTP5添加时,您将能够在IQueryable上调用Include!

我从来没有对EF QL或其他什么东西有过太多的爱。。。你能试试吗:var results=ctx.Posts.Include(x=>x.Comments)。Where(x=>x.Blogs.Any(b=>b.Owner.EmailAddress==)alexj@microsoft.com"));