SplitOn column is needed twice in the query to work properly
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)
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
This shows TestParentId = 0
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
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