[英]Map a nested object using Dapper
In my project I have two classes在我的项目中,我有两个课程
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; }
and this one和这个
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; }
}
And I want to execute this query我想执行这个查询
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
and map the result of this query to DashboardNodeData object和 map 这个查询的结果到 DashboardNodeData object
Using dapper Im doing in this way使用 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;
}
The problem is That inside de Query function, my node object always come empty:问题是在 de Query function 中,我的节点 object 总是为空:
But if I copy and paste the exact same query in the database it is returned the output:但是,如果我在数据库中复制并粘贴完全相同的查询,则会返回 output:
What Am I doing Wrong?我究竟做错了什么?
First, you have Node and DashboardNodeData switched.首先,您切换了 Node 和 DashboardNodeData。 Dapper will map the first split to the first generic type and the second to the second and so on. Dapper 将 map 第一个拆分为第一个泛型,第二个拆分为第二个,依此类推。
Secondly, you don't actually want to split on County, the latitude, longitude, county, and district are part of the same object (DashboardNodeData).其次,您实际上并不想在县上进行拆分,纬度、经度、县和区是同一个 object (DashboardNodeData) 的一部分。 It doesn't actually matter what tables the data is coming from, just the objects that you want to map to.实际上,数据来自什么表并不重要,只是您想要 map 到的对象。
var dashboard = db.Query<Node, DashboardNodeData, DashboardNodeData>(command, (node, dash) =>
{
dash.Node = node;
return dash;
}, splitOn:"Latitude").AsList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.