dapper加入splitOn多列键

时间:2019-03-11 09:15:19

标签: dapper

我正在一家拥有巨大数据库的新公司中工作... 我正在尝试将dapper引入orm ...

我在两个表之间具有1-N的关系,并且具有多个PK

我的查询类似于

SELECT * 
FROM testmag INNER JOIN movmag  ON 
movmag.code= testmag.code AND
 tm_type = mm_type 
 AND tm_year = mm_year
 AND tm_serie = mm_serie
 AND tm_documentNumber = mm_documentNumber

和我的表类模型:

public class testMag
{
        [Key]
        [Column(Order = 0)]
        public string code{ get; set; }

        [Key]
        [Column(Order = 1)]
        public string tm_type { get; set; }

        [Key]
        [Column(Order = 2)]
        public short tm_year { get; set; }

        [Key]
        [Column(Order = 3)]
        public string tm_serie { get; set; }

        [Key]
        [Column(Order = 4)]
        public int tm_documentNumber { get; set; }

        ...
}

public class movMag
{
    [Key]
    [Column(Order = 0)]
    public string code { get; set; }

    [Key]
    [Column(Order = 1)]
    public string mm_type { get; set; }

    [Key]
    [Column(Order = 2)]
    public short mm_year { get; set; }

    [Key]
    [Column(Order = 3)]
    public string mm_serie { get; set; }

    [Key]
    [Column(Order = 4)]
    public int mm_documentNumber { get; set; }

    [Key]
    [Column(Order = 5)]
    public int mm_row { get; set; }

    ... 
}

我正在尝试使用仅一个splitOn键和所有键的精简查询

 string qJoin2 = "SELECT top 10 * FROM testmag INNER JOIN movmag  ON movmag.code= testmag.code AND tm_type = mm_type AND tm_year = mm_year  AND tm_serie = mm_serie  AND tm_documentNumber = mm_documentNumber  ";

            var res = connection.Query<movmag, testmag, Tuple<movmag, testmag>>(
                qJoin2,
                Tuple.Create,
                splitOn: "code,tm_type,tm_year,tm_serie,tm_documentNumber,code,mm_type,mm_year,mm_serie,mm_documentNumber,mm_row").ToList();


            var res2 = connection.Query<movmag, testmag, Tuple<movmag, testmag>>(
                qJoin2,
                Tuple.Create,
                splitOn: "code").ToList();

我错了吗? 有人可以解释我! 非常感谢

1 个答案:

答案 0 :(得分:0)

我在dapper.query()中更改了类型顺序 而且可以同时使用

  var res = connection.Query<testmag, movmag, Tuple<testmag, movmag>>(
                    qJoin2,
                    Tuple.Create,
                    splitOn: "codditt,tm_tipork,tm_anno,tm_serie,tm_numdoc,codditt,mm_tipork,mm_anno,mm_serie,mm_numdoc,mm_riga").ToList();

            var res2 = connection.Query<testmag, movmag, Tuple<testmag, movmag>>(
                qJoin2,
                Tuple.Create,
                splitOn: "codditt").ToList();