问题描述
刚开始使用Dapper并喜欢它.我遇到了问题,它返回了正确数量的对象,但是它们的属性都具有默认值
just started to use Dapper and like it.I'm having a problem with it, it returns the right number of objects, butthey all have default values for their properties
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对象的列表,所有这些对象都具有默认属性,而不是数据库中的值
chipTimes just has a list of ChipTime objects, all of which just have the default properties and not the values from the database
如果我这样做
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();
}
所有动态参数都具有正确的值
The dynamics all have the right values in
ChipTime类非常简单
ChipTime class is really simple
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 *:
Try to specify the names of columns that you want to select in your query instead of doing a SELECT *:
const string sQuery2 = @"SELECT TimeId, TimingPoint, MacAddress, ChipCode, ... FROM ChipTime WHERE MacAddress = @MacAddress AND ClientId = @ClientId ORDER BY CreateDate Desc";
确保ChipTime表中的列与ChipTime类的属性名称完全匹配,并指定类型参数(dbConnection.Query < ChipTime> ):
Make sure that the columns in your ChipTime table exactly matches the property names of your ChipTime class and specify the type argument (dbConnection.Query <ChipTime>):
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();
}
这篇关于Dapper强类型查询返回默认对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!