在我的项目中,我有两个类。
问题是在 Query 函数内,我的节点对象总是为空: 但是,如果我在数据库中复制并粘贴完全相同的查询,则返回以下输出: 我做错了什么?
public class Node
{
public int IdNode { get; set; }
public string Description { get; set; }
public int IdLocation { get; set; }
public int IdNearStation { get; set; }
public bool IsEnable { get; set; }
public bool IsRealSensor { get; set; }
public bool IsSprinklerON { get; set; }
public bool IsLightOn { get; set; }
和这一个
public class DashboardNodeData
{
public Node Node { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public string County { get; set; }
public string DistrictName { get; set; }
}
我想执行这个查询
SELECT N.IdNode
, N.Description
, N.IdLocation
, N.IdNearStation
, N.IsEnable
, N.IsRealSensor
, N.IsSprinklerON
, N.IsLightOn
, N.IsSecurityCameraOn
, L.Latitude
, L.Longitude
, C.Name as County
, D.DistrictName
FROM [dbo].[Node] N
inner join [dbo].[Location] L on N.IdLocation = L.Id_Location
inner join District D on D.Id_District=L.Id_District
inner join Counties C on L.Id_Countie =C.CountyId
where N.IdNode=1
请将此查询的结果映射到DashboardNodeData对象
使用Dapper,我是这样做的
using (IDbConnection db = new SqlConnection(_connectionString))
{
string command = $@"
SELECT N.IdNode
, N.Description
, N.IdLocation
, N.IdNearStation
, N.IsEnable
, N.IsRealSensor
, N.IsSprinklerON
, N.IsLightOn
, N.IsSecurityCameraOn
, L.Latitude
, L.Longitude
, C.Name as County
, D.DistrictName
FROM [dbo].[Node] N
inner join [dbo].[Location] L on N.IdLocation = L.Id_Location
inner join District D on D.Id_District=L.Id_District
inner join Counties C on L.Id_Countie =C.CountyId
where N.IdNode=@idNode".Replace("@idNode",idNode.ToString());
var dashboard = db.Query<DashboardNodeData, Node, DashboardNodeData>(command, (dash, node) =>
{
dash.Node = node;
return dash;
}, splitOn:"Latitude,County").AsList();
return dashboard;
}
问题是在 Query 函数内,我的节点对象总是为空: 但是,如果我在数据库中复制并粘贴完全相同的查询,则返回以下输出: 我做错了什么?