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
Dapper.SqlMapper.AddTypeHandler is not working as I would expect.
You will need to add the following PackageReference to your csproj (see dotnet/efcore#28124), but this issue is related to the mapping itself, not the strict table support - strict tables just show the problem better.
usingMicrosoft.Data.Sqlite;usingSystem.Data;usingDapper;namespaceBugReport{publicclassDateTimeToTimestampHandler:SqlMapper.TypeHandler<DateTimeOffset?>{publicoverridevoidSetValue(IDbDataParameterparameter,DateTimeOffset?value){Console.WriteLine($"SetValue was hit, input value is {value.Value}");parameter.Value=value.Value.ToUnixTimeSeconds();}publicoverrideDateTimeOffset?Parse(objectvalue){Console.WriteLine($"Parse was hit, input value is {value} - converted to {DateTimeOffset.FromUnixTimeSeconds((long)value)}");returnDateTimeOffset.FromUnixTimeSeconds((long)value);}}publicclassBugReport{publicstaticvoidMain(){DateTimeOffset?myTimestamp=DateTimeOffset.UtcNow;Dapper.SqlMapper.AddTypeHandler(newDateTimeToTimestampHandler());varconnection=newSqliteConnection("Data Source=:memory:");connection.Open();SQLitePCL.raw.sqlite3_trace(connection.Handle,(_,statement)=>Console.WriteLine($"Sent to SQLite: {statement}"),null);Console.WriteLine("SQLite version is "+connection.ExecuteScalar("SELECT sqlite_version()"));connection.Execute("CREATE TABLE BugReport (ThisIsAnIntColumn INTEGER) STRICT");connection.Execute("INSERT INTO BugReport Values (1653915600)");varfirstSelect=connection.Query<DateTimeOffset?>("SELECT * FROM BugReport");Console.WriteLine($"Mapped result is {firstSelect.First()}");try{connection.Execute("INSERT INTO BugReport VALUES (@MyTimestamp)",new{MyTimestamp=myTimestamp});}catch(Exceptione){Console.WriteLine(e.Message);}}}}
Expected: DateTimeToTimestampHandler.SetValue is called, INTEGER (long) value inserted into the table
Actual:
Sent to SQLite: SELECT sqlite_version()
SQLite version is 3.38.3
Sent to SQLite: CREATE TABLE BugReport (ThisIsAnIntColumn INTEGER) STRICT
Sent to SQLite: INSERT INTO BugReport Values (1653915600)
Sent to SQLite: SELECT * FROM BugReport
Parse was hit, input value is 1653915600 - converted to 30/05/2022 1:00:00 PM +00:00
Mapped result is 30/05/2022 1:00:00 PM +00:00
Sent to SQLite: INSERT INTO BugReport VALUES ('2022-05-30 13:03:01.4304754+00:00')
SQLite Error 19: 'cannot store TEXT value in INTEGER column BugReport.ThisIsAnIntColumn'.
SetValue is never called, and ToString is called on the DateTimeOffset instead.
It looks like Dapper.SqlMapper.LookupDbType(System.Type type, string name, bool demand, out Dapper.SqlMapper.ITypeHandler handler) will never actually resolve my type handler.
Dapper.SqlMapper.AddTypeHandler is not working as I would expect.
You will need to add the following
PackageReference
to your csproj (see dotnet/efcore#28124), but this issue is related to the mapping itself, not the strict table support - strict tables just show the problem better.Expected:
DateTimeToTimestampHandler.SetValue
is called, INTEGER (long) value inserted into the tableActual:
SetValue is never called, and ToString is called on the DateTimeOffset instead.
I'm using the following versions:
on .NET 6.
The text was updated successfully, but these errors were encountered: