c# - DbContext.Set().SqlQuery 和 DbContext.Database.SqlQuery返回不同的结果
问题描述
SQL查询:
SELECT 0 AS Id,
u.UserID AS UserId,
u.first_name AS FirstName,
u.last_name AS LastName,
uf.uf_facility_id AS FacilityId
FROM ...
实体没有额外的映射,只是在 DbContext 中定义了属性 - public DbSet<UserFacility> UserFacilities { get; set; }
:
public class UserFacility
{
public int Id { get; set; }
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int FacilityId { get; set; }
}
DbContext.Set().SqlQuery
返回每个项目的重复位置列表,UserFacility
它UserId
等于UserId
第一行的值,例如:
Id UserId FirstName LastName FacilityId
0 33057 ggtest 2 3938
0 33057 QaAuOne TestOne 3938
0 33057 QaAuOne TestOne 3928
DbContext.Database.SqlQuery
返回预期结果(与在 SSMS 中运行此查询时得到的结果相同)。
Id UserId FirstName LastName FacilityId
0 33057 ggtest 2 3938
0 33098 QaAuOne TestOne 3938
0 33098 QaAuOne TestOne 3928
有人可以解释到底发生了什么以及我如何DbContext.Set<T>().SqlQuery
才能返回正确的数据吗?我正在使用 EntityFramework 6.2.0。
提前致谢。
解决方案
推荐阅读
- python - 输入包含 NaN、无穷大或对于 Django 项目中的 dtype('float64') 而言太大的值
- python - Windows 10 中的“无法从‘信号’导入名称‘SIGPIPE’”
- scala - 如何从顶点输入文件创建 VertexRDD?
- ruby-on-rails - 我可以在我的代码的一个 simple_form 中禁用 select2 吗?
- java - Reactive Mongo - DeleteAllBy... 查询 - 找不到类型类 java.lang.Void 的 PersistentEntity
- java - Java 节点交换
- php - 尝试在 woocommerce 的结帐页面中的总部分之后放置自定义字段
- sql-server-2016 - DATEPART MILLISECONDS 从 COMPATIBILITY_LEVEL 120 到 130 的差异
- node.js - 保护我的 ExpressJS 服务器路由,它们将通过 cURL 访问
- r - 如何在没有表达式功能的情况下将上标添加到 ggplot 标签?