You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Problem with DateOnly mapping when updating form v2.1.35 to 2.1.44 #2071
I used to have this custom type handler configured: SqlMapper.AddTypeHandler(new DapperSqlDateOnlyTypeHandler());
Mapping class is:
public class DapperSqlDateOnlyTypeHandler : SqlMapper.TypeHandler<DateOnly>
{
public override void SetValue(IDbDataParameter parameter, DateOnly date)
=> parameter.Value = date.ToDateTime(new TimeOnly(0, 0));
public override DateOnly Parse(object value)
=> DateOnly.FromDateTime((DateTime)value);
}
This was working fine, but looks like the DateOnly support was added in 2.1.37 but
Now I am getting this error:
System.Data.DataException
Error parsing column 1 (Date=669745 - Int32)
at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in /_/Dapper/SqlMapper.cs:line 3948
Inner exception:
InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.DateOnly'.
Thing is, the Date column is column 1, but somehow is parsing column 0 value
Query result: 669745 2023-11-01 NULL NULL ...
So I am a bit confused, in v2.1.35 I didn't have this problem. I removed custom mapper SqlMapper.AddTypeHandler(new DapperSqlDateOnlyTypeHandler()); and still same problem.
Edit:
In another query, I set date only field as first one: 2023-11-01 0 0
The error is:
System.Data.DataException
Error parsing column 0 (Date=n/a - Unable to cast object of type 'System.DateTime' to type 'System.DateOnly'.)
at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in /_/Dapper/SqlMapper.cs:line 3948
at Deserialize68929759-6271-4665-a68e-ff25a40601eb(DbDataReader)
at Dapper.SqlMapper.QueryAsync[T](IDbConnection cnn, Type effectiveType, CommandDefinition command)
The class is:
public record TestClass
{
public DateOnly Date { get; set; }
public int First { get; set; }
public int Second { get; set; }
}
@GonzaloVisma Do you use the System.Data.SqlClient or Microsoft.Data.SqlClient in a combination with Dapper? I have been using the first one with the same workaround you listed above. But recently I have switched to Microsoft.Data.SqlClient and workaroud is not required any more.
@GonzaloVisma Do you use the System.Data.SqlClient or Microsoft.Data.SqlClient in a combination with Dapper? I have been using the first one with the same workaround you listed above. But recently I have switched to Microsoft.Data.SqlClient and workaroud is not required any more.
I am using Microsoft.Data.SqlClient already, looks like versions > v2.1.35 are unlisted now, maybe they were several issues with those versions an this was one of them.
Also, someone else reported this issue after me #2072 and in that thread there are more answers of this same problem
I have been unable to get a repro of this failing; for now I guess I have no choice except to revert that change, but if anyone who was seeing this problem could show a runnable repro against 2.1.44 (or similar affected), I'd be very grateful
I have been unable to get a repro of this failing; for now I guess I have no choice except to revert that change, but if anyone who was seeing this problem could show a runnable repro against 2.1.44 (or similar affected), I'd be very grateful
i am experiencing the same issue; i could not produce a runnable repro; but i tried to pin point it; afaik, there is no direct cast from DateTime to DateOnly:
MSSQL might have a suitable data type that can be casted as DateOnly but i am working with ORACLE and this is not the case here.
i had a custom converter for DateOnly which worked fine before 2.1.44:
with 2.1.44 my custom converter never gets invoked and i get the cast error.
btw, i expected custom converter to take precedence but that did not work as i expected; so i reverted back to 2.1.35
I can't stop you doing that, but keep in mind that 2.1.44 is unlisted:
which leads onto the answer to your question:
Why can't we just fix it?
Because:
it isn't that simple - "fixing" something for some people at the cost of breaking things for other people: doesn't work
at the current time, I simply haven't had time to go through and figure out every possible break/fix scenario, and thread the needle of finding a solution that works for everyone
almost every moment of effort on this comes from the private-time (i.e. not work time) of a tiny handful of people, mostly me, and our (my) time is finite and limited
there may exist a temporary workaround via AOT, where I have (today) merged the DateTime etc pieces
you say you have an ugly workaround that works for today: great! do that! that's better than me deploying things in a sometimes-broken state, until I can get to the bottom of things
I'm sorry, I didn't mean to be rude. I appreciate the work you and your team do, and I didn't mean to belittle it—please disregard my last sentence.
(By the way, I use a private corporate feed over which I have no control, and the latest version available is 2.1.44. I'll see about notifying my boss to remove it. Thank you, and sorry again)
I would like to add my 2cents, .44 worked for me. Reverting in .66 broke my code. So now I'm in .44 limbo. I am using Microsoft.Data.SqlClient. I would suggest others update their client with new DateOnly support vs reverting back to no support.
To reiterate: 2.1.44 was unlisted a very long time ago because the DateOnly etc handling had inconsistent and unreliable results - I will try to revisit this to reinstate that logic, but if 2.1.44 works for you (it didn't work for everyone): maybe keep using that for now.
Any update on this? Just ran into it after upgrading from 2.1.44 to 2.1.66. Unfortunately can't downgrade as previous version is not listed, is there any work arounds?
2.1.44 is still there; you can simply use 2.1.44 in your csproj/props and everything will continue to work. I've been on vacation while all this has kicked off - will be looking when I am available. If you're using UI tools that don't list 2.1.44: just open the csproj (etc) as text files and find the Dapper <PackageReference...>
Activity
mkorsukov commentedon Apr 22, 2024
@GonzaloVisma Do you use the
System.Data.SqlClientorMicrosoft.Data.SqlClientin a combination with Dapper? I have been using the first one with the same workaround you listed above. But recently I have switched toMicrosoft.Data.SqlClientand workaroud is not required any more.glebron-woffu commentedon Apr 23, 2024
I am using Microsoft.Data.SqlClient already, looks like versions > v2.1.35 are unlisted now, maybe they were several issues with those versions an this was one of them.
Also, someone else reported this issue after me #2072 and in that thread there are more answers of this same problem
mkorsukov commentedon Apr 23, 2024
@GonzaloVisma Here is my current setup for data-related project:
glebron-woffu commentedon Apr 23, 2024
Mine was
mgravell commentedon Apr 26, 2024
I have been unable to get a repro of this failing; for now I guess I have no choice except to revert that change, but if anyone who was seeing this problem could show a runnable repro against 2.1.44 (or similar affected), I'd be very grateful
disable DateOnly / TimeOnly support - see #2071 / #2072
disable DateOnly / TimeOnly support - see #2071 / #2072 (#2080)
disable DateOnly / TimeOnly support - see #2071 / #2072
akurone commentedon Jul 6, 2024
hello @mgravell,
i am experiencing the same issue; i could not produce a runnable repro; but i tried to pin point it; afaik, there is no direct cast from

DateTimetoDateOnly:MSSQL might have a suitable data type that can be casted as
DateOnlybut i am working with ORACLE and this is not the case here.i had a custom converter for

DateOnlywhich worked fine before 2.1.44:with 2.1.44 my custom converter never gets invoked and i get the cast error.
btw, i expected custom converter to take precedence but that did not work as i expected; so i reverted back to 2.1.35
hope this helps.
ThomasHeijtink commentedon Nov 8, 2024
Is there already an update on this issue?
glebron-woffu commentedon Nov 8, 2024
I'm affraid there is no news...
I am still using the customs handlers
And registering them in program.cs
DottorPagliaccius commentedon Feb 7, 2025
I managed to make it work with 2.44.
For years we've been stuck with this ugly workaround in every project. Why can't we just fix it?
mgravell commentedon Feb 7, 2025
I can't stop you doing that, but keep in mind that 2.1.44 is unlisted:
which leads onto the answer to your question:
Because:
DottorPagliaccius commentedon Feb 7, 2025
I'm sorry, I didn't mean to be rude. I appreciate the work you and your team do, and I didn't mean to belittle it—please disregard my last sentence.
(By the way, I use a private corporate feed over which I have no control, and the latest version available is 2.1.44. I'll see about notifying my boss to remove it. Thank you, and sorry again)
RobK410 commentedon Feb 11, 2025
I would like to add my 2cents, .44 worked for me. Reverting in .66 broke my code. So now I'm in .44 limbo. I am using Microsoft.Data.SqlClient. I would suggest others update their client with new DateOnly support vs reverting back to no support.
mgravell commentedon Feb 11, 2025
I will try to review what went wrong here and get us back into a working state.
mkorsukov commentedon Feb 11, 2025
I'm on Dapper
2.1.44and Microsoft.Data.SqlClient6.0.1and everything works fine. I don't use anySqlMapperrelated workarounds.However, upgrading Dapper to
2.1.66breaks DateOnly/TimeOnly support.Micha-kun commentedon Feb 14, 2025
Not only with SqlClient, with MySqlConnector 2.4.0 too. Dapper 2.1.66 gives error, but 2.1.44 doesn't.
mgravell commentedon Feb 14, 2025
To reiterate: 2.1.44 was unlisted a very long time ago because the DateOnly etc handling had inconsistent and unreliable results - I will try to revisit this to reinstate that logic, but if 2.1.44 works for you (it didn't work for everyone): maybe keep using that for now.
victorprocure commentedon Feb 14, 2025
Any update on this? Just ran into it after upgrading from 2.1.44 to 2.1.66. Unfortunately can't downgrade as previous version is not listed, is there any work arounds?
mgravell commentedon Feb 15, 2025
2.1.44 is still there; you can simply use 2.1.44 in your csproj/props and everything will continue to work. I've been on vacation while all this has kicked off - will be looking when I am available. If you're using UI tools that don't list 2.1.44: just open the csproj (etc) as text files and find the Dapper
<PackageReference...>aki-seppanen commentedon Feb 27, 2025
We had issues on Npgsql with DateOnly/TimeOnly not working on .66, while .44 seems to work fine.
ielcoro commentedon May 14, 2025
Me too having issues with DateOnly/TimeOnly on DuckDB, falling back to .44 solves the problem for the time being
gabrielsoldani commentedon Dec 12, 2025
It looks like Npgsql 10.0 now returns
DateOnlyinstead ofDateTimefordatecolumns by default causing problems with Dapper 2.1.66. I haven't tested yet if reverting back to 2.1.44 fixes it.Re-enable DateOnly/TimeOnly support, fixing the defects that got it d…