我有一个名为 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/