-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reading nullable DateTime fails when the column has a non-null value #295
Comments
Is this a MySql only issue, because I've never seen this problem using sql server or sql ce? |
From my perspective it very well could be. I've yet to run into it on any of the SQL Server environments I've used Dapper on. |
I had the same problem working with mysql and nullable DateTime. I had this in my connection string:
and I removed the second part
to make the error go away. I could do that because I don't need that setting, so maybe it's not that simple for you. |
Hi guys, I had the exact same settings on my connection string. I took out the "Allow Zero Datetime=true", seems that this solves it. Additionally, if I look at the documentation https://dev.mysql.com/doc/connector-net/en/connector-net-connection-options.html , I don't think "Convert Zero Datetime" and "Allow Zero Datetime" should be used at the same time as they seem to be doing different things. In the mean time, I upgraded Dapper and Mysql to the latest NuGet packages and the error still happens when "Allow Zero Datetime" is present. For my personal case, adapting the connection string is acceptable. Thanks for the help! |
I would be interested in solving the problem more generally. I'll have a look at how the existing code differed, and what we can do here. I agree the right thing is to just work. |
Added test rig with all 4 permutations; it worked each time. Cannot reproduce. Could do with some help seeing this. |
I can assure that problem occurs with Sqlite v3 & Dapper 1.42 & 1.52. I have two properties on my model to the same column
|
I have the same problem has @ShadyAbuKalam and i don't find a solution. |
@ShadyAbuKalam |
As per issue DapperLib#295, setting "AllowZeroDateTime=True" in the connection string causes an InvalidCastException in SqlMapper.
* Don't create the connection in Dispose: this avoids creating a SQLConnection simply to Dispose it. * Run MySQL tests against the open MySqlConnection: 'connection' is a protected variable in the TestBase class that is a SQL Server connection, not a MySQL connection. * Skip broken MySQL tests: as per issue #295, setting "AllowZeroDateTime=True" in the connection string causes an InvalidCastException in SqlMapper.
Cannot reproduce with MySQL.Data 6.10.6 and Dapper 1.50.4 on .NET Framework 4.6.2 |
Before this commit, the following exception would be thrown: > System.InvalidCastException > Unable to cast object of type 'MySqlConnector.MySqlDateTime' to type 'System.Nullable`1[System.DateTime]'. > at Deserialize74d21eca-a78e-49dc-b635-d007c613cb51(DbDataReader ) Fixes DapperLib#295
Before this commit, the following exception would be thrown: > System.InvalidCastException > Unable to cast object of type 'MySqlConnector.MySqlDateTime' to type 'System.Nullable`1[System.DateTime]'. > at Deserialize74d21eca-a78e-49dc-b635-d007c613cb51(DbDataReader ) Fixes DapperLib#295
I think I finally found an easy solution to make dates just work with MySQL in #1916. |
I'm using MySql database and the NuGet version of Dapper. I'm reading from the database like this:
The class contains a nullable DateTime property. When the value is NULL in the database, it works fine. When it has a value, it fails complaining:
I managed to solve this by installing my own TypeHandler for
DateTime?
. I saw how Dapper.NodaTime is doing it so I created my own handler:and I make sure I install this handler before anything else happens:
With this in place, I can read nullable dates correctly whether they have a NULL value or an actual value. I think this should be built-in functionality provided by Dapper, so I'm raising this ticket.
The stacktrace I get without my workaround looks like this:
The text was updated successfully, but these errors were encountered: