0

I have two SQL tables: Teams and Members. Each team contains 3 members, in the database the members' ids are stored.

Sample Database

How could I map the Member objects into the Teams using the Dapper.NET ORM?

public class Team
{
    public int? id { get; set; }
    public Member MemberA { get; set; }
    public Member MemberB { get; set; }
    public Member MemberC { get; set; }
}

public class Member
{
    public int? id { get; set; }
    public string Name { get; set; }
}



public IEnumerable<Team> GetTeams()
{
    string sql = "SELECT * FROM Teams t LEFT JOIN Members m ON t.MemberA=m.id AND t.MemberB=m.id AND t.MemberC=m.id";

    return m_connection.Query<Team, Member, Member, Member, Team>(sql, (t, m1, m2, m3) =>
    {
        t.MemberA = m1;
        t.MemberB = m2;
        t.MemberC = m3;

        return t;
    }, splitOn: "MemberA,MemberB,MemberC");
}
3
  • 4
    Shouldn't your team have a List<Member>? Commented Aug 14, 2019 at 9:39
  • 1
    This is more a Sql syntax problem than a Dapper one Commented Aug 14, 2019 at 9:43
  • 1
    @stuartd No, it's not a one-to-many relationship. Commented Aug 14, 2019 at 9:49

1 Answer 1

3

You need to fix your sql query to have a proper join with the Members Table.

Just change it to

string sql = @"SELECT t.ID, t.MemberA, m1.Id, m1.Name, 
                            t.MemberB, m2.Id, m2.Name, 
                            t.MemberC, m3.Id, m3.Name
               FROM Teams t LEFT JOIN Members m1 ON t.MemberA=m1.id 
                            LEFT JOIN Members m2 ON t.MemberB=m2.id
                            LEFT JOIN Members m3 ON t.MemberC=m3.id";

and your dapper code will work as you expect filling the three Member instance of every single Team retrieved.

Notice that when you use multimapping, you need to place the SplitOn elements in the proper place to have Dapper understand your requirement to create three different Member variables.

Version for MS-Access

string sql = @"SELECT t.ID, t.MemberA, m1.Id, m1.[Name], 
                            t.MemberB, m2.Id, m2.[Name], 
                            t.MemberC, m3.Id, m3.[Name]
               FROM (((Teams t LEFT JOIN Members m1 ON t.MemberA=m1.id) 
                            LEFT JOIN Members m2 ON t.MemberB=m2.id)
                            LEFT JOIN Members m3 ON t.MemberC=m3.id)";
Sign up to request clarification or add additional context in comments.

10 Comments

Does this sql expression works with OleDbConnection too? Because I got a syntax error.
What database are you using?
MS Access database
Probably it is the column titled Name. It is a reserved keyword in MS-Access. Try to use m1.[Name]....,m2.[Name],...,m3.[Name]
Paste the query into Access and it should tell you what it doesn't like
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.