简体   繁体   中英

Using Dapper with multiple inner joins

I am trying to use Dapper with the following sql string but I am unable to get it work:

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";

I have the following model classes:

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; }
}

I createad a custom group class hoping to map easier those models:

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

}

I am trying to use Dapper to get results I want like this:

                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");

but I am unable to see the big picture and what I am doing wrong because it throws an exception

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

I am able to use Dapper with a simpler (one) inner join sql string, but this one I cannot get it work.

I reviewed the code and I've come to the conclusion that you simply are not including the fields you need to split on. Add the following fields to your query (perhaps adding distinct labels for the types that share similar field names.

PROGRAMID, OBJECTID, GROUPID, NAME

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM