首页 新闻 会员 周边

已添加了具有相同键的项

0
[待解决问题]

action.SelectList 报错,
数据源是多表联查的sql语句,或者是视图的时候
报错
System.ArgumentException: 已添加了具有相同键的项
联查的表或视图,都有主键id,select tolist<>() 的时候会有多个id列

示例:

public class T_tmp
{
    public int id { get; set; }
    public string title { get; set; }

    public string content { get; set; }
}


internal class Program
{
    static void Main(string[] args)
    {
        string connString = $"Server=localhost;Database=ufdata_999_2014;User ID=sa;Password=123;";
        string sql = "SELECT a.id,a.title,b.content FROM dbo.t_tmp1 a LEFT JOIN dbo.t_tmp2 b ON a.id = b.pid";
        using (MAction action = new MAction(sql,connString))
        {
            List<T_tmp> list = action.SelectList<T_tmp>();
        }
        Console.ReadKey();
    }
}

/**************
CREATE TABLE t_tmp1
(
id INT IDENTITY(1,1),
title NVARCHAR(100)
,PRIMARY KEY(id)
)
GO

CREATE TABLE t_tmp2(
id INT IDENTITY(1,1),
pid int ,
content NVARCHAR(100)
,PRIMARY KEY(id)
)
GO

INSERT INTO dbo.t_tmp1(title)
VALUES(N'abc' -- title - nvarchar(100)
)
INSERT INTO dbo.t_tmp2(pid, content)
VALUES(1, -- pid - int
N'aaaaaa' -- content - nvarchar(100)
)
SELECT a.id,a.title,b.content FROM dbo.t_tmp1 a LEFT JOIN dbo.t_tmp2 b ON a.id = b.pid
*************/

oo0595的主页 oo0595 | 菜鸟二级 | 园豆:202
提问于:2023-08-26 19:13
< >
分享
所有回答(2)
0

本地试过了,这是个神奇的问题,应该是微软ado.net底层新产生的bug,框架这边做了兼容处理,源码已更新了,见源码:ConvertTool.cs

路过秋天 | 园豆:4787 (老鸟四级) | 2023-08-26 23:16
0

用 reader 的 Read() 方法,读取数据并移到下一条数据,否则一直都是读第一条
while (reader.Read())
{
}

geass.. | 园豆:1821 (小虾三级) | 2023-10-20 10:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册