本文介绍了如何使用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;  
});

您可以找到有关其多对象映射的文档此处.我应该指出,除非另有说明,否则它会在ID上分开,并且对象在查询结果中出现的顺序也是如此.

I want to split data into two separate models with DAPPER ORM in C#, From TeamId to Location between a column in one model and RowNumber to PageCount into another sperate model.

解决方案

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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 00:58