c# - 为什么 Entity Framework 在从 View 中选择时创建子查询?

标签 c# mysql entity-framework .net-3.5 linq-to-entities

我有一个名为 PersonTable 的表,其中包含以下列:PersonId、RestarauntId、Age

我有一个名为 PersonView 的 View :

select PersonId, 
       RestarauntId, 
       RestarauntName(RestarauntId) as `RestarauntName`, 
       Age 
FROM PersonTable

当我做一些简单的事情时:

var persons = context.PersonView.Where(x=>x.PersonId == 1)
                                .Select(x=> 
                                   new {x.PersonId, 
                                        x.RestarauntId, 
                                        x.RestarauntName, 
                                        x.Age });

以上返回 1 条记录,我希望 MySql 查询是:

SELECT PersonId, RestarauntId, RestarauntName, Age 
FROM PersonView
WHERE PersonId = 1

但是,它会生成以下内容:

SELECT 1 AS `C1`, T.PersonId, T.RestarauntId, T.RestarauntName, T.Age
FROM
(SELECT PersonId, RestarauntId, RestarauntName, Age 
FROM PersonView) AS T
WHERE T.PersonId = 1

所以无论我传递给 where 子句什么,它总是会首先在子选择中获取所有记录。只有当我查询我需要的 View 时才会发生这种情况,但我很好奇为什么它会创建上述查询而不是我期望它创建的查询?这是 Entity Framework 问题还是 MySql 问题?

最佳答案

MySql View 不允许在查询中使用动态过滤器。
很少有技巧可以实现这一点。但是根据设计,mysql View 本质上不是动态的。 View 始终执行提供的实际查询,并且仅在该结果上执行进一步过滤,如您在示例中提到的那样。更多详情,请访问Here

关于c# - 为什么 Entity Framework 在从 View 中选择时创建子查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17751870/

相关文章:

mysql - 如何生成唯一的随机数?

c# - Entity Framework where、order 和 group

c# - Code First 问题,使用西里尔文播种数据

c# - 如何在我的项目中重用 xaml 命名空间定义

c# - 为什么在有效的双输入时收到 FormatException?

php - 两个表一起查询,只显示一条记录

.net - Entity Framework 和应用程序架构(松耦合等)

c# - Task.ContinueWith 不适用于 OnlyOnCanceled

c# - 如何检索保存在 mysql 中的 asp.net 中的路径作为链接?

mysql - 如何获取与 Sequelize 生成的模型相关的数据?