我可以在EF 4.1执行一个查询标? [英] Can I execute a scalar query in EF 4.1?
问题描述
是否有可能执行使用标量值查询EF 4.1?
Is it possible to execute a scalar-valued query using EF 4.1?
我试过:
ObjectQuery<int> query = new ObjectQuery<int>("select count(*) from myTable", _context);
var result = query.Execute(MergeOption.NoTracking);
return result.FirstOrDefault();
但它返回一个错误:查询语法无效。近期'*'
是执行标量值查询调用存储过程的唯一途径?
Is the only way to execute a scalar-valued query to call a stored proc?
推荐答案
在我看来,你所想的实体框架为ADO.net 2.0,而不是它是ORM。
It seems to me that you are thinking of entity framework as an "ADO.net 2.0" rather than the ORM that it is.
而不是用它来执行SQL查询,我建议你使用标准的实体框架数据容器和使用LINQ查询,如打算与实体框架。然后,它只会是这样
Rather than using it to execute SQL queries, I'd recommend using the standard entity framework Data container and using a LINQ query, as is intended with entity framework. Then it would simply be something like
myDataContainerInstance.myTable.Count()
的ObjectQuery是不是类似于ADO.net命令并没有执行SQL语句。相反,它是用来定义针对直接对象模型的查询。因为它不是SQL中的(*)是无效的。
ObjectQuery isn't analogous to the ADO.net command and doesn't execute SQL statements. Instead it is used to define queries against the object model directly. The (*) isn't valid because it isn't SQL.
这篇关于我可以在EF 4.1执行一个查询标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!