问题描述
实体框架 6 中的动态 sql 查询是否可以返回 IQueryable
?
Is there something that can return IQueryable
for a dynamic sql query in Entity Framework 6?
这是我现在使用的,但它正在提取所有记录(如预期的那样).
This is what I am using now but it is pulling all the records (as expected).
DbContext.Database.SqlQuery<T>("SELECT * FROM dbo.SomeDynamicView")
问题是 SqlQuery
返回 DbRawSqlQuery
即IEnumerable
.
dbo.SomeDynamicView
是在运行时创建的数据库视图.
dbo.SomeDynamicView
is a database view created at runtime.
推荐答案
不,你不能从 SqlQuery
获取 IQueryable
,这是因为 IQueryable
正在做的是根据您输入的选择和位置过滤器动态构建 SQL 字符串.因为在 SqlQuery
您提供的字符串 Entity Framework 可以不生成该动态字符串.
No, you can't get a IQueryable
from SqlQuery
, this is because what IQueryable
is doing is building a SQL string dynamically based on what select and where filters you put in. Because in SqlQuery
you are providing the string Entity Framework can not generate that dynamic string.
您的选择是动态构建您自己的字符串以传递给 SqlQuery
并将其用作 IEnumerable
而不是 IQueryable
或在您的 DbContext
中使用 DbSet
并以更正常"的方式让实体框架为您构建查询.
Your options are either dynamically build the string your self to pass in to SqlQuery
and use it as a IEnumerable
instead of a IQueryable
or use a DbSet
in your DbContext
and do the more "normal" way of letting entity framework build the query for you.
这篇关于使用 SqlQuery 获取 IQueryable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!