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
How to use alternate type "REAL" (JulianDay) for DateTimes in EFcore? #24714
Btw: HasConversion (on 5.0.5) only accepts expressions .HasConversion(dt => ToJulianDate(dt), real => FromJulianDate(real)) - or do I miss an additional namespace/assembly for additional extension methods? (there are so many of them).
@springy76 This is by design. The expressions are inserted into the larger materialization expression, which is in turn compiled into a delegate for fast materialization.
The values are stored as real "REAL" now 👍 (using .HasColumnType("REAL").HasConversion(julianDateConverter))
But I'm not quite happy with the queries
var now = DateTime.UtcNow;
context.Elements.Where(e => e.Added > now).ToList();
context.Elements.Where(e => e.Added > DateTime.UtcNow).ToList();
First one uses string parameter (DbType = String) but at least a julian day floating point value:
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (0ms) [Parameters=[@__now_0='2459332.189668947' (DbType = String)], CommandType='Text', CommandTimeout='30']
SELECT "e"."ElementId", "e"."Added", "e"."Kind", "e"."Name", "e"."ParentId", "e"."HashgroupId", "e"."IntegrationCategoryId"
FROM "Elements" AS "e"
WHERE "e"."Added" > @__now_0
and for the second one I doubt anything will create a JulianDate before comparing real-value with string-value: WHERE "e"."Added" > rtrim(rtrim(strftime('%Y-%m-%d %H:%M:%f', 'now'), '0'), '.')
The second query was just for testing, won't use that for anything productive.
Activity
ajcvickers commentedon Apr 23, 2021
/cc @bricelam
bricelam commentedon Apr 23, 2021
Looks like changing DbParameter.DbType would require a custom type mapping...
Instead, you can copy the logic into a value converter:
efcore/src/Microsoft.Data.Sqlite.Core/SqliteValueBinder.cs
Lines 249 to 271 in 71f14c8
efcore/src/Microsoft.Data.Sqlite.Core/SqliteValueReader.cs
Lines 295 to 325 in 71f14c8
springy76 commentedon Apr 26, 2021
Would be handy when those static functions would get public for the future.
bricelam commentedon Apr 26, 2021
We discussed this as a team, and decided that it would be good if the EF Core SQLite provider handled this automatically.
.Property(e =>e.Char).HasColumnType("INTEGER").Property(e =>e.DateTime).HasColumnType("REAL").Property(e =>e.DateTimeOffset).HasColumnType("REAL").Property(e =>e.Guid).HasColumnType("BLOB").Property(e =>e.TimeSpan).HasColumnType("REAL")📝 Note to implementor: The SQL literal format should also reflect the mapping.
springy76 commentedon Apr 27, 2021
Please consider providing provider-specific extension methods which don't use strings for the 4-5 native types SQLite interprets.
springy76 commentedon Apr 27, 2021
Btw: HasConversion (on 5.0.5) only accepts expressions
.HasConversion(dt => ToJulianDate(dt), real => FromJulianDate(real))- or do I miss an additional namespace/assembly for additional extension methods? (there are so many of them).ajcvickers commentedon Apr 27, 2021
@springy76 This is by design. The expressions are inserted into the larger materialization expression, which is in turn compiled into a delegate for fast materialization.
springy76 commentedon Apr 27, 2021
Ok... I additionally had to create an explicit converter to use it against nullable
DateTime?fields as well:(I somewhere did read that converters always ignore null?)
ajcvickers commentedon Apr 27, 2021
Yes,
springy76 commentedon Apr 27, 2021
The values are stored as real "REAL" now 👍 (using
.HasColumnType("REAL").HasConversion(julianDateConverter))But I'm not quite happy with the queries
First one uses string parameter (DbType = String) but at least a julian day floating point value:
and for the second one I doubt anything will create a JulianDate before comparing real-value with string-value:
WHERE "e"."Added" > rtrim(rtrim(strftime('%Y-%m-%d %H:%M:%f', 'now'), '0'), '.')The second query was just for testing, won't use that for anything productive.
bricelam commentedon Apr 27, 2021
The method translators will also need to be updated to understand the conversion.