如何将Entity框架中的DbSet转换为ObjectQuery [英] How to convert DbSet in Entity framework to ObjectQuery

查看:473
本文介绍了如何将Entity框架中的DbSet转换为ObjectQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CodeFirst方法,并遇到一个问题,我需要将DbSet转换为ObjectQuery。
这是我做的转换。

I am using CodeFirst approach and struck with an issue where I need to convert DbSet to ObjectQuery. This is what I did for conversion.

ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext;
ObjectSet<Request> objectSet = objectContext.CreateObjectSet<Request>();

其中db是从DbContext继承的上下文,Request是类。

where db is the context inheriting from DbContext and Request is class.

所以,当我尝试调用ObjectQuery作为ObjectQueryMethod(objectSet)的方法时,会抛出以下错误。

So, when I try to call the method that expects ObjectQuery as ObjectQueryMethod(objectSet), it throws the following error.

条件类型无法确定表达式,因为'System.Data.Entity.DbSet<>'和'System.Data.Objects.ObjectQuery<>'之间没有隐式转换

"Type of conditional expression cannot be determined because there is no implicit conversion between 'System.Data.Entity.DbSet<>' and 'System.Data.Objects.ObjectQuery<>'"

任何帮助非常感谢!

推荐答案

我找到答案。当然,可以使用下面的代码行将Entity框架中的DbSet转换为ObjectQuery。

I found the answer. Of course, it is possible to convert DbSet in Entity framework to ObjectQuery using the below lines of code.

ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext;  
ObjectSet<Request> objectSet = objectContext.CreateObjectSet<Request>("Requests");

其中,


  • db - 从 DbContext 继承的上下文类。在Context类中定义的

  • 请求 - DbSet< Request>

  • objectSet - 现在可以以 ObjectQuery
  • db - Context class inherting from DbContext.
  • Requests - DbSet<Request> defined in Context class.
  • objectSet - Can now be passed as ObjectQuery.

这篇关于如何将Entity框架中的DbSet转换为ObjectQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆