使用 Dapper 映射嵌套对象

Map a nested object using Dapper

在我的项目中我有两个类

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;

            }

问题是在 de Query 函数中,我的节点对象总是空的:

但是如果我在数据库中复制并粘贴完全相同的查询,则会返回输出:

我做错了什么?

首先,您切换了 Node 和 DashboardNodeData。 Dapper 会将第一个拆分映射到第一个泛型,将第二个拆分映射到第二个,依此类推。

其次,您实际上并不想在县上进行拆分,纬度、经度、县和区是同一个对象(DashboardNodeData)的一部分。数据来自哪个表实际上并不重要,重要的是您要映射到的对象。

var dashboard = db.Query<Node, DashboardNodeData, DashboardNodeData>(command, (node, dash) =>
                {
                    dash.Node = node;
                    return dash;
                }, splitOn:"Latitude").AsList();