DapperLib / DapperLib/Dapper

SplitOn column is needed twice in the query to work properly

Open
#2,155 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs-triage
Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

Hello,

I'm using Dapper 2.1.66 with SQL Server and Net 8.
I noticed a weird thing using the SplitOn feature where it doesn't work as expected.

I created a basic test case to demonstrate the issue.
I have created 2 basic tables with just a few columns in each:

  • TestParent: TestParentId, TestParentNm
  • TestChild: TestChildId, TestParentId, PermissionId

I use this query to pull the data:

Select p.TestChildId, p.PermissionId, p.TestParentId, r.TestParentNm 
From TestChild p inner join TestParent r ON p.TestParentId = r.TestParentId

As shown in the Dapper SplitOn example page, I only select the join column once (see: https://www.learndapper.com/relationships#dapper-spliton)

This gives this result (in green, child columns, in blue, parent columns)

Image

Now, in my code, when I want to query the data and populate my objects, I use this code:

            var query = "Select p.TestChildId, p.PermissionId, p.TestParentId, r.TestParentId, r.TestParentNm " +
                "From TestChild p inner join TestParent r ON p.TestParentId = r.TestParentId";
            var itemList = (await dbDapperHelper
                .GetDapperConnection()
                .QueryAsync<TestChild, TestParent, TestChild>(
                query, (child, parent) => {
                    child.TestParent = parent;
                    return child;
                },
                new { },
                splitOn: "TestParentId"))
                .ToList();

GetDapperConnection() is a helper method that returns the SqlConnection

Ok, now the weird thing.
When I run this code, I'm able to populate "TestParent" with the parent object, but the TestParentId of the child object remains 0

Image
This shows TestParentId = 0

Image
But here you can see that the TestParent object is populated, and has a TestParentId of 4.
I would expect the TestParentId in the child to be 4 too.

Now, if I modify the SQL query to output the TestParentId column twice, it works as expected

Image

This strikes me as weird, since the example in the official Dapper documentation shows that the splitOn column needs to be in the output columns only once.

Is there something in my set up that is wrong?
Let me know if you need more details

Thanks

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at Dapper's QueryAsync multi-mapping path and the splitOn handling, then reproduce the supplied SQL Server example with the TestChild and TestParent objects. Compare behavior when TestParentId is selected once versus twice; done when the documented single split column preserves the child's TestParentId, with a regression test covering the case.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.