简体   繁体   中英

Dapper is converting my dates into 01-Jan-01 and I cannot see why?

I'm using SQL Server 2012 and Dapper v1.50.5.

I have a standard query:

SELECT * 
FROM [TsData].[ImportingLogs] 
WHERE ImportContextGuid = '{3c19d706-0895-49e4-b96c-38eb6a3cc579}'

which returns some data:

在此处输入图片说明

and the CreationTime column I am interested in has valid datetimes.

The table is simply defined as:

在此处输入图片说明

The POCO is:

public class OzCsImportingLogsTableModel
{
    public DateTime CreationDateTime { get; set; }
    public int? CreatorUserId { get; set; }
    public int? DeleterUserId { get; set; }
    public DateTime DeletionTime { get; set; }
    public int DurationMs { get; set; }
    public int Id { get; set; }
    public Guid ImportContextGuid { get; set; }
    public bool IsDeleted { get; set; }
    public DateTime? LastModificationTime { get; set; }
    public int? LastModifierUserId { get; set; }
    public string Message { get; set; }
    public OzCsImportManagerMessageKindEnum MessageKindId { get; set; }
    public string Source { get; set; }
    public string StructuredData { get; set; }
    public string Tags { get; set; }
}

and the Dapper call is:

DbContext.Execute($"[{Schema}].[usp_ImportingQueue_FinaliseImport]", storedProcParams, aCommandType: CommandType.StoredProcedure)

However when I look at OzCsImportingLogsTableModel.CreationTime , the value is always 01-Jan-01 00:00:00 which indicates the value is NULL.

I don't understand this. Can someone point me in the right direction here please?

@John has it correct in his comment. The name of the property (here CreationDateTime ) generally must match the name of the column (here CreationTime ). Having that said, it is not really the name of the table column, but the name of the result column, so you could do something like this:

SELECT CreationTime as CreationDateTime, ...

if you can modify the actual query.

As commented by @fstam: the behavoir is that 01-jan-01 is the default value of a DateTime and since it's a non-nullable type and never set to anything by dapper, it is the value shown.

Note that the logic applied to find the member for a column name is available here :

// preference order is: exact match over underscore match, exact case over wrong case, backing fields over regular fields, match-inc-underscores over match-exc-underscores

In your case, however none of the above applies; it is probably best to adjust the name of the property in code.

Update:

Another thing that I just saw: the DurationMs column in your schema is nullable, but the DurationMs property is not. You might want to define this property as public int? DurationMs { get; set; } public int? DurationMs { get; set; } public int? DurationMs { get; set; } instead. (I haven't checked all members).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM