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
*************/
本地试过了,这是个神奇的问题,应该是微软ado.net底层新产生的bug,框架这边做了兼容处理,源码已更新了,见源码:ConvertTool.cs
用 reader 的 Read() 方法,读取数据并移到下一条数据,否则一直都是读第一条
while (reader.Read())
{
}