Mapping multiple objects in dapper using Split-on and Query Multiple together

自闭症网瘾萝莉.ら 提交于 2019-12-13 03:47:14

问题


Consider Role-Employee-Address

An employee can have only one role but can have many addresses. So using split on is not efficient as we may get the duplicates of roles and employee. We can use query multiple, but i feel if there is a way we can capture the role and employee together in one result and address in another, then that would be better.

In that case, instead of returning role and employee separately, we can directly join both in a single query and split on some column while mapping.

I'm expecting something like this

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 do like this using both the techniques together?


回答1:


Correct, you need a One-To-Many mapping which is not natively supported by Dapper, but can be easily implemented if the database you're using supports JSON. Here's an article I wrote (along with working samples) that shows how you can do it using SQL Server / Azure SQL:

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



来源:https://stackoverflow.com/questions/53757053/mapping-multiple-objects-in-dapper-using-split-on-and-query-multiple-together

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!