无法翻译 EF7 和 GroupBy()

EF7 and GroupBy() cannot be translated

我在 EF7 Beta 8 上有以下代码 运行ning:

var locationGrops = from l in db.Locations
                    group l by l.ServiceType into g
                    select g;

var list = locationGrops.ToList();

当我执行这段代码时,EF 显示警告。

warning : [Microsoft.Data.Entity.Query.QueryCompilationContext] The LINQ express
ion 'GroupBy([l].ServiceType, [l])' could not be translated and will be evaluate
d locally.

这个查询对我来说似乎很基础,SQL 中有 GROUP BY。有什么方法可以在服务器上 运行 吗?

此时 group by 和大多数子查询不受 EF7 支持。

您可以使用 context.Locations.FromSql(sql).ToList() 来确保您的查询 运行 在服务器上如您所愿。

一种方法是创建一个具有逻辑的数据库视图(对于复杂的分组可能更好)。

现在使用视图有点麻烦,但如果您使用脚手架,这就是我想到的:

How can I create database Views in a scaffolded DbContext in EF7 / Entity Framework Core 1.0

总而言之,我继承了 DbContext,并为视图手动创建实体 class。