数据库列值的Dapper错误值

时间:2016-02-18 00:35:46

标签: c# dapper

我正在使用Dapper和C#来获取商店程序的结果。 表(TPTable)以Pid作为主键,Value作为第二列和其他一些列的名称。 我有一个商店:select * from TPTable

当我调用QueryAsync<TPTableModel>方法时,结果值Value Column的值总是与主键相同

在DB

中说

Pid = 1,值= 2

Pid = 2,值= 568

结果将是{{Pid = 1,Value = 1},{Pid = 2,Value = 2}}的列表。

为什么我每次都会Value等于PrimaryKey?

2 个答案:

答案 0 :(得分:1)

这在本地工作正常:

[Fact]
public async void SO35470588_WrongValuePidValue()
{
    // nuke, rebuild, and populate the table
    try { connection.Execute("drop table TPTable"); } catch { }
    connection.Execute(@"
create table TPTable (Pid int not null primary key identity(1,1), Value int not null);
insert TPTable (Value) values (2), (568)");

    // fetch the data using the query in the question, then force to a dictionary
    var rows = (await connection.QueryAsync<TPTable>("select * from TPTable"))
        .ToDictionary(x => x.Pid);

    // check the number of rows
    rows.Count.IsEqualTo(2);

    // check row 1
    var row = rows[1];
    row.Pid.IsEqualTo(1);
    row.Value.IsEqualTo(2);

    // check row 2
    row = rows[2];
    row.Pid.IsEqualTo(2);
    row.Value.IsEqualTo(568);
}
public class TPTable
{
    public int Pid { get; set; }
    public int Value { get; set; }
}

我很乐意调查,但非常感谢repro。

答案 1 :(得分:1)

我发现了问题。与Dapper不同。我是repro一个小项目,我发现商店有一个内部联接与SQL用户定义类型。此类型具有名为Value的列,其具有主键值。当我用商店上的select *调用Dapper时,它将填充用户定义类型的值。

PrintScreen in sqlserver