How in Dapper to map multiples entities with SplitOn and QueryMultiple?

dapper

Question

Think about zzz-5 zzz

An employee may have more than one address but only one position. Therefore, employing split on is inefficient since it may result in employee and job duplication. While we may utilize query many, I believe it would be preferable if there was a method to capture the job and employee in one return and address in another.

In such instance, we may immediately join both in a single query and split on some field when mapping, as opposed to returning role and employee separately.

I anticipate something along these lines.

string query = "StoredProcedure";

using (var multi = connection.QueryMultiple(query, null))
{
    empRole = multi.Read<Employee,Role>().Single().SplitOn("Id");
    add = multi.Read<Address>().ToList();
}    

Is there any way we could combine the two methods to achieve this?

1
0
3/8/2019 5:18:31 PM

Accepted Answer

It's true that you need a One-To-Many mapping, which Dapper does not naturally provide but which can be readily created if your database also supports JSON. Here is a post I prepared that demonstrates how to accomplish it using SQL Server and Azure SQL, complete with functioning samples:

https://medium.com/dapper-net/one-to-many-mapping-with-dapper-55ae6a65cfd4

0
12/13/2018 6:55:24 PM


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow