升级到版本6后,带有动态查询的Entity Framework异常

时间:2019-12-06 14:20:18

标签: c# entity-framework linq entity-framework-6

我有一个使用EF的应用程序。最初它可以正常工作,但是当EF升级到6时,其中一个动态查询已开始引发异常。这就是commandText的内容:

SELECT VALUE q FROM SELECT 
[Extent1].[column1] AS [column1], 
[Extent1].[column2] AS [column2], 
[Extent1].[column3] AS [column3]
FROM (SELECT 
[viewPPS].[column1] AS [column1], 
[viewPPS].[column2] AS [column2], 
[viewPPS].[column3] AS [column3]
FROM [dbo].[viewPPS] AS [viewPPS]) AS [Extent1] as q 

var result = pqb.RunQueryQ(); 

它给出异常“查询语法无效。靠近关键字'SELECT',第1行,第21列”

当我更新此字符串时,commandText变为:

SELECT q.* FROM (SELECT 
[Extent1].[column1] AS [column1], 
[Extent1].[column2] AS [column2], 
[Extent1].[column3] AS [column3]
FROM (SELECT TOP 10
[viewPPS].[column1] AS [column1], 
[viewPPS].[column2] AS [column2], 
[viewPPS].[column3] AS [column3]
FROM [dbo].[viewPPS] AS [viewPPS]) AS [Extent1]) as q 

但是错误仍然相同。该查询的正确语法应该是什么?

这是用于构建查询的代码:

ParameterQueryBuilder<viewPPS> pqb =
new ParameterQueryBuilder<viewPPS>(context);
string aliasName = "q";

pqb.AddSelect("SELECT VALUE " + aliasName + " FROM " + context.viewPPS.Sql +
" as " + aliasName + " where ", aliasName);

ParameterQueryBuilder.cs:

public class ParameterQueryBuilder<TEntity> : ParameterQueryBuilder where TEntity : class
{
public ParameterQueryBuilder(DbContext context)
: base(context)
{
}

public IEnumerable<TEntity> RunQuery()
{
return base.RunQuery<TEntity>();
}

public IQueryable<TEntity> RunQueryQ()
{
return base.RunQueryQ<TEntity>();
}
}

0 个答案:

没有答案