【发布时间】:2021-05-11 10:31:19
【问题描述】:
据我了解,以下代码应生成仅包含 RouteId、RouteNo 和 ShipId 的查询
var tow = (from t in _context.AllTowData
where t.RouteId == id
orderby t.RouteNo descending
select new TowDefaults {
Id = t.RouteId,
TowNo = t.RouteNo,
ShipId = t.ShipId,
LastTow = t.RouteNo
})
.FirstOrDefault();
但是,我得到:
SELECT v.route_id, v.route_no, v.tow_id, v.analysis_complete, v.checks_complete, v.cpr_id, v.date_created, v.date_last_modified, v.factor, v.fromportname, v.instrument_data_file, v.instrument_id, v.internal_number, v.mastername, v.message, v.miles_per_division, v.month, v.number_of_samples, v.number_of_samples_analysed_fully, v.prop_setting, v.route_status, v.sampled_mileage, v.serial_no_per_calendar_month, v.ship_speed, v.silk_reading_end, v.silk_reading_start, v.toportname, v.tow_mileage, v.validity, v.year
FROM view_all_tow_data AS v
WHERE v.route_id = '@__id_0'
ORDER BY v.route_no DESC
LIMIT 1
这是每一列除了明确要求的ShipId!我做错了什么?
同时使用 SQL Server 和 PostGres 数据库会发生这种情况
【问题讨论】:
-
AllTowData的 exact 类型是什么?此外,您应该显示类模型 + 自定义映射代码(如果存在)。 -
当您在
Select中包含一些未映射的([NotMappped]或.Ignore(...)d)源列时,通常会发生这种情况。在这种情况下,ShipId似乎是配置为被忽略的属性。 -
选择成匿名类型
.Select(t => new { t.RouteId, TowNo = t.RoueNo }); -
@BjarkeHandsdal 我看不出匿名类型有什么帮助,但是......它没有:-(我试过了
标签: entity-framework-core entity-framework-core-3.1