将Dapper与多个内部联接一起使用

时间:2019-04-13 23:10:04

标签: c# sql inner-join dapper

我正在尝试将Dapper与以下sql字符串一起使用,但无法正常工作:

string groupsStringDetailed = "SELECT SUSERGROUP.NAME, SUSERGROUP.DESCRIPTION, SPROGRAMS.PROGRAMNAME, SOBJECTS.NAME FROM ((SIDE.SADMIT SADMIT " +
        "INNER JOIN SIDE.SOBJECTS SOBJECTS ON (SADMIT.PROGRAMID=SOBJECTS.PROGRAMID) AND (SADMIT.OBJECTID=SOBJECTS.ID)) " +
        "INNER JOIN SIDE.SUSERGROUP SUSERGROUP ON SADMIT.GROUPID=SUSERGROUP.GROUPID) " +
        "INNER JOIN SIDE.SPROGRAMS SPROGRAMS ON SOBJECTS.PROGRAMID=SPROGRAMS.ID " +
        "WHERE SUSERGROUP.NAME NOT LIKE '%REPORT' ORDER BY SUSERGROUP.NAME, SPROGRAMS.PROGRAMNAME";

我有以下模型类:

public class SAdmit
{
    public int GROUPID { get; set; }
    public int OBJECTID { get; set; }
    public int PROGRAMID { get; set; }

}

public class SObjects
{
    public int ID { get; set; }
    public int PROGRAMID { get; set; }
    public string NAME { get; set; }

}

public class SPrograms
{
    public int ID { get; set; }
    public string PROGRAMNAME { get; set; }
}

public class SUserGroup
{
    public int GROUPID { get; set; }
    public string NAME { get; set; }
    public string DESCRIPTION { get; set; }
    public int VWLISTDEPTH { get; set; }
    public int WDNBDAYHISTORY { get; set; }
    public string RPDIRECTORY { get; set; }
    public string SENDEREMAIL { get; set; }
    public int CONNECTION_TIMEOUT { get; set; }
    public int APPROVALSTATUS { get; set; }
}

我创建了一个自定义组类,希望能够更轻松地映射那些模型:

public class CustomSGroup
{
    public SUserGroup Group { get; set; }
    public SPrograms Programs { get; set; }
    public SObjects Objects { get; set; }

}

我正在尝试使用Dapper获得我想要的结果:

                var output = await cnn.QueryAsync<CustomSGroup, SAdmit, SObjects, SPrograms, CustomSGroup>(groupsStringDetailed, (g, a, o, p) =>
                {
                    a.PROGRAMID = o.PROGRAMID;
                    a.OBJECTID = o.ID;
                    a.GROUPID = g.Group.GROUPID;
                    o.PROGRAMID = p.ID;
                    return g;
                }, splitOn: "PROGRAMID, OBJECTID, GROUPID, NAME");

但是我看不到全局图片和我做错了什么,因为它引发了异常

"When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id\r\nParameter name: splitOn"

我可以将Dapper与一个更简单的(一个)内部连接sql字符串一起使用,但是我无法使用它。

1 个答案:

答案 0 :(得分:0)

我查看了代码,得出的结论是,您只是不包括需要分割的字段。在查询中添加以下字段(可能为共享相似字段名称的类型添加不同的标签。

PROGRAMID, OBJECTID, GROUPID, NAME