gpt4 book ai didi

c# - Dapper 强类型查询返回默认对象值

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:40 24 4
gpt4 key购买 nike

刚刚开始使用 Dapper 并且喜欢它。我遇到了问题,它返回正确数量的对象,但它们的属性都有默认值

using (var dbConnection = Connection)
{
await dbConnection.OpenAsync();

const string sQuery2 = @"SELECT * FROM ChipTime WHERE MacAddress = @MacAddress AND ClientId = @ClientId ORDER BY CreateDate Desc";
var chipTimes = dbConnection.Query<ChipTime>(sQuery2, new { ClientId = clientId, MacAddress = id }).ToList();
}

chipTimes 只有一个 ChipTime 对象列表,所有这些对象都只有默认属性,而不是数据库中的值

如果我这样做

using (var dbConnection = Connection)
{
await dbConnection.OpenAsync();

const string sQuery2 =
@"SELECT * FROM ChipTime WHERE MacAddress = @MacAddress AND ClientId = @ClientId ORDER BY CreateDate Desc";
var chipTimes = dbConnection.Query(sQuery2, new {ClientId = clientId, MacAddress = id}).ToList();
}

动态都具有正确的值

ChipTime类真的很简单

    public class ChipTime
{
public int TimeId { get; set; }
public string TimingPoint { get; set; }
public string MacAddress { get; set; }
public string ChipCode { get; set; }
public DateTime ChipDateTime { get; set; }
public DateTime ReaderTime { get; set; }
public int ReaderTimeDec { get; set; }
public string UhfReaderNo { get; set; }
public string Rssi { get; set; }
public DateTime CreateDate{ get; set; }
public int ReplStatus { get; set; }
public int RaceId { get; set; }
public int ClientId { get; set; }
public int RecordNo { get; set; }
public string AntennaNo { get; set; }
public bool IsRaceNo { get; set; }

}

我做错了什么?

最佳答案

尝试指定要在查询中选择的列的名称,而不是执行 SELECT *:

const string sQuery2 = @"SELECT TimeId, TimingPoint, MacAddress, ChipCode, ... FROM ChipTime WHERE MacAddress = @MacAddress AND ClientId = @ClientId ORDER BY CreateDate Desc";

Why is SELECT * considered harmful?

I tried this but same results.

确保您的 ChipTime 表中的列与您的 ChipTime 类的属性名称完全匹配并指定类型参数(dbConnection.Query ):

using (var dbConnection = Connection)
{
await dbConnection.OpenAsync();

const string sQuery2 =
@"SELECT TimeId, TimingPoint, MacAddress, ChipCode, ... FROM ChipTime WHERE MacAddress = @MacAddress AND ClientId = @ClientId ORDER BY CreateDate Desc";
var chipTimes = dbConnection.Query<ChipTime>(sQuery2, new {ClientId = clientId, MacAddress = id}).ToList();
}

关于c# - Dapper 强类型查询返回默认对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41149588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com