如何使用Dapper ORM将SQL Select查询拆分为两个Model C# [英] How to split a SQL Select query into two Model C# with Dapper ORM

查看:242
本文介绍了如何使用Dapper ORM将SQL Select查询拆分为两个Model C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用C#中的DAPPER ORM将数据分为两个单独的模型,从一个模型中的列之间的TeamIdLocation,以及从RowNumberPageCount的另一个模型中的数据.

解决方案

我相信您要模拟的对象将是:

public class Team
{
     public int Id { get; set; }
     public string Name { get; set; }
     public Supervisor Supervisor { get; set; }
     public Location Location { get; set; }
}

实际上,您可以使用Dapper最多实现七个对象,基本上您在SQL中会执行以下操作:

public IEnumerable<Team> GetTeams() => dbConnection.Query<Team, Supervisor, Location, Team>(query, 
(team, supervisor, location, team) => 
{
   team.Supervisor = supervisor,
   team.Location = location,
   return team;  
});

您可以找到有关其多对象映射的文档 解决方案

I believe what you are trying to simulate would be an object such as:

public class Team
{
     public int Id { get; set; }
     public string Name { get; set; }
     public Supervisor Supervisor { get; set; }
     public Location Location { get; set; }
}

You can actually achieve this with Dapper up to seven objects, basically what you would do in your SQL would be:

public IEnumerable<Team> GetTeams() => dbConnection.Query<Team, Supervisor, Location, Team>(query, 
(team, supervisor, location, team) => 
{
   team.Supervisor = supervisor,
   team.Location = location,
   return team;  
});

You can find documentation on their multi object mapping here. I should denote it splits on Id unless specified and the order the objects appear in query result matter.

这篇关于如何使用Dapper ORM将SQL Select查询拆分为两个Model C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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