一起使用拆分和查询多个在dapper中映射多个对象 [英] Mapping multiple objects in dapper using Split-on and Query Multiple together

查看:117
本文介绍了一起使用拆分和查询多个在dapper中映射多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑角色-员工地址

员工只能担任一个角色,但可以有多个地址。因此,使用拆分方式效率不高,因为我们可能会获得角色和员工的重复项。我们可以使用多个查询,但是我觉得如果有一种方法可以在一个结果中同时捕获角色和员工,并在另一个结果中进行寻址,那会更好。

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();
}    

是否可以同时使用这两种技术来做到这一点? / p>

Is there any way we could do like this using both the techniques together?

推荐答案

正确,您需要一对多映射,Dapper本身不支持该映射,但是如果您正在使用的数据库支持JSON。这是我写的文章(连同工作示例),展示了如何使用SQL Server / Azure SQL进行操作:

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

这篇关于一起使用拆分和查询多个在dapper中映射多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆