{"action":"opened","issue":{"url":"https://api.github.com/repos/StackExchange/dapper-dot-net/issues/295","labels_url":"https://api.github.com/repos/StackExchange/dapper-dot-net/issues/295/labels{/name}","comments_url":"https://api.github.com/repos/StackExchange/dapper-dot-net/issues/295/comments","events_url":"https://api.github.com/repos/StackExchange/dapper-dot-net/issues/295/events","html_url":"https://github.com/StackExchange/dapper-dot-net/issues/295","id":85805669,"number":295,"title":"Reading nullable DateTime fails when the column has a non-null value","user":{"login":"ngeor","id":461097,"avatar_url":"https://avatars.githubusercontent.com/u/461097?v=3","gravatar_id":"","url":"https://api.github.com/users/ngeor","html_url":"https://github.com/ngeor","followers_url":"https://api.github.com/users/ngeor/followers","following_url":"https://api.github.com/users/ngeor/following{/other_user}","gists_url":"https://api.github.com/users/ngeor/gists{/gist_id}","starred_url":"https://api.github.com/users/ngeor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ngeor/subscriptions","organizations_url":"https://api.github.com/users/ngeor/orgs","repos_url":"https://api.github.com/users/ngeor/repos","events_url":"https://api.github.com/users/ngeor/events{/privacy}","received_events_url":"https://api.github.com/users/ngeor/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-06-06T19:43:35Z","updated_at":"2015-06-06T19:43:35Z","closed_at":null,"body":"I'm using MySql database and the NuGet version of Dapper. I'm reading from the database like this:\r\n\r\n\treturn _connection.Query<Person>(\r\n\t\t\"SELECT * FROM Person WHERE PersonId=@PersonId\",\r\n\t\tnew { PersonId = 42 }).SingleOrDefault();\r\n\r\nThe 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:\r\n\r\n    Error parsing column 14 (LastCommentedAt= - Object)\r\n\r\nI 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:\r\n\r\n\tpublic class NullableDateTimeHandler : SqlMapper.TypeHandler<DateTime?>\r\n\t{\r\n\t\tprotected NullableDateTimeHandler()\r\n\t\t{\r\n\t\t}\r\n\r\n\t\tpublic static readonly NullableDateTimeHandler Default = new NullableDateTimeHandler();\r\n\r\n\t\tpublic override void SetValue(IDbDataParameter parameter, DateTime? value)\r\n\t\t{\r\n\t\t\tif (value.HasValue)\r\n\t\t\t{\r\n\t\t\t\tparameter.Value = value.Value;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tparameter.Value = DBNull.Value;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tpublic override DateTime? Parse(object value)\r\n\t\t{\r\n\t\t\tif (value == null)\r\n\t\t\t{\r\n\t\t\t\treturn null;\r\n\t\t\t}\r\n\r\n\t\t\tif (value is DateTime)\r\n\t\t\t{\r\n\t\t\t\treturn (DateTime)value;\r\n\t\t\t}\r\n\r\n\t\t\treturn Convert.ToDateTime(value);\r\n\t\t}\r\n\t}\r\n\r\n\r\nand I make sure I install this handler before anything else happens:\r\n\r\n\tSqlMapper.AddTypeHandler(NullableDateTimeHandler.Default);\r\n\r\nWith 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.\r\n\r\nThe stacktrace I get without my workaround looks like this:\r\n\r\n\tMESSAGE:\r\n\tSystem.Data.DataException : Error parsing column 14 (LastCommentedAt= - Object)\r\n\t  ----> System.InvalidCastException : Cannot cast from source type to destination type.\r\n\t+++++++++++++++++++\r\n\tSTACK TRACE:\r\n\tat Dapper.SqlMapper.ThrowDataException (System.Exception,int,System.Data.IDataReader,object) <0x002e7>\r\n\tat (wrapper dynamic-method) object.Deserializedf762666-acf8-4d08-832f-f40f4dc76cd7 (System.Data.IDataReader) <0x0105e>\r\n\tat Dapper.SqlMapper/<QueryImpl>d__61`1<Person>.MoveNext () <0x006c3>\r\n\tat System.Collections.Generic.List`1<Person>.AddEnumerable (System.Collections.Generic.IEnumerable`1<Person>) <0x0008f>\r\n\tat System.Collections.Generic.List`1<Person>..ctor (System.Collections.Generic.IEnumerable`1<Person>) <0x000d5>\r\n\tat System.Linq.Enumerable.ToList<Person> (System.Collections.Generic.IEnumerable`1<Person> <0x0004b>\r\n\tat Dapper.SqlMapper.Query<Person> (System.Data.IDbConnection,string,object,System.Data.IDbTransaction,bool,System.Nullable`1<int>,System.Nullable`1<System.Data.CommandType>) <0x001fb>\r\n\tat PersonDataLayer.Read (int) <0x0007f>\r\n\tat PersonTests.TestCreate () <0x004ef>\r\n\r\n\t--InvalidCastException\r\n\tat System.Nullable`1<System.DateTime>.Unbox (object) <0x00081>\r\n\tat (wrapper dynamic-method) object.Deserializedf762666-acf8-4d08-832f-f40f4dc76cd7 (System.Data.IDataReader) <0x00f3d>\r\n\r\n"}}