用于3个一对一关系的Dapper多重映射

问题描述 投票:0回答:1

我在Sql Server中有3个表,一对一关系。

键是所有3个表中的Indice。

[3个实体:

public class Client
{
     public int Indice { get; set; }
     public string Name { get; set; }
     public string Surname { get; set; }
     public string Company { get; set; }
     public string Tel1 { get; set; }
     public string Tel2 { get; set; }
}

public class CallClient
{
     public int Indice { get; set; }
     public string CallDateTime { get; set; }
     public string Status { get; set; }
}

public class ResponsePollClient
{
     public int Indice { get; set; }
     public string Question1 { get; set; }
     public string Question2 { get; set; }
     public string Question3 { get; set; }
     public string StatusPoll { get; set; }
}

我有这个主要实体

public class DataClient
{
     public Client client { get; set; }
     public CallClient callClient { get; set; }
     public REsponsePollClient pollClient { get; set; }
}

SQL:

选择c.Indice,....,cc.Indice,...,pc.Indice,...来自客户c在c.Indice = cc.Indice上内部加入CallClient ccc.Indice = pc.Indice上的内部加入PollClient pc

如何使用Dapper填充列表实体?

c# dapper multi-mapping
1个回答
0
投票

以下代码应映射您的pocos:

   var sql = @"SELECT c.Indice 
             , c.Name
             , ....
             , cc.Indice
             , cc.CallDateTime 
             , ....
             , rpc.Indice 
             , rpc.Question1 
             , .... 
        FROM Client c
  INNER JOIN CallClient cc on c.Indice = cc.Indice
  INNER JOIN PollClient pc on c.Indice = pc.Indice";

        using (var conn = new SqlConnection())
        {
            var res = conn.Query<Client, CallClient, ResponsePollClient, DataClient>(sql, (c, cc, pc) =>
            {
                var dataClient = new DataClient
                {
                    client = c,
                    callClient = cc,
                    pollClient = pc
                };

                return dataClient;
            }, splitOn: "Indice,Indice,Indice");
        }
© www.soinside.com 2019 - 2024. All rights reserved.