Skip to content

DateOnly "off by one" column error #2072

@edwardmjackson

Description

@edwardmjackson

Hi,

Using Microsoft.Data.SqlClient 5.2.0 and Dapper 2.1.44, we've noticed an issue with the new DateOnly type support from #2051.
Using a simple class such as

  public class PersonTest
  {
    public string FirstName { get; set; }
    public DateOnly FromDate { get; set; }
  }

and a QueryAsync with

    public async Task<PersonTest> GetPerson(string Username)
    => (await QueryAsync<PersonTest>(AdminProcs.GetPerson, new { Username })).FirstOrDefault();

we get an error response of

{
  "type": "System.Data.DataException",
  "title": "Dapper",
  "status": 500,
  "detail": "Error parsing column 1 (FromDate=Ed - String)",
  "traceId": "00-ae3a4c30e843c63d927302fde275a754-ca340c148ee9f15f-00"
}

The data, returned by MS SQL Server, as a varchar and DateTime type, are as follows:

FirstName                                          FromDate
-------------------------------------------------- -----------------------
Ed                                                 2019-10-01 00:00:00.000

The issue seems to be that it is trying to parse column 1 as the FromDate with the value of Ed, which is in column 0. Hence the "off by one" error.

Running this with Dapper 2.1.35, using the the previous custom solution from #1715, does not have this error.

Staying on 2.1.44, if we remove FromDate from PersonTest, it works fine with just the FirstName.
Changing FromDate in PersonText to a more open type, such as string, also works fine.
Changing the SQL type, as e.g., as MS SQL Date, or an int or varchar in ISO8601 format, doesn't have any effect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions