It seems to me that NodaTime is becoming a defacto library for many C# projects, much like Json.Net due to its superior handling of DateTime concepts. I feel it would be very beneficial if there was native support for NodaTime built into EFCore. I have attempted to make it work with ValueConverters but there are some current limitations: #10434, #11156. Even once those issues are resolved it would still only support the converting of the types but not necessarily allow using any SQL functions related to DateTime types, ie DATEPART, DATEADD etc.
To that end I have created a repo here that is an initial attempt at adding support for the major types:
The reason for this ticket is 2-fold:
Moving to efcore repo
@StevenRasmussen Yep, this has been on our mind for a while now. Note that Npgsql already has support for NodaTime. /cc @roji
Npgsql docs: http://www.npgsql.org/efcore/mapping/nodatime.html
I wanted to provide an update on the project. As of v1.2.0 I was able to get the most common operations working for all of the types, ie DATEADD and DATEPART is supported for Instant, OffsetDateTime, LocalDateTime, LocalDate, LocalTime, and Duration. Thanks @bricelam for your work on a solution for HierarchyId which I used as a resource for getting this all hooked up... and of course thanks to everyone on the EF Core team for a great codebase that allows for this plugin scenario without needing to modify any of the source!
For more information see the repo here: https://github.com/StevenRasmussen/EFCore.SqlServer.NodaTime
@StevenRasmussen really nice to see this! Some quick comments:
Just for background, in Npgsql NodaTime support is built into the ADO layer (via a plugin system separate from EF Core's). This allows the EF Core provider to pass NodaTime types down directly without any value converters.
@roji - Thanks for the feedback! Definitely agree on the name so I have updated it to EFCore.SqlServer.NodaTime as you suggested. Regarding your second point: Is this with regards to the HasData concept? Or DataMigrations? Currently the unit test project uses HasData to successfully seed some data in a localDb. I also just tested creating a migration and that appears to have worked as well... but I'm probably missing something. Thanks!
@StevenRasmussen right, sorry - because your type mappings use value converters, you don't need to worry about code literal generation - migrations get code literals for the converted types (e.g. DateTime). In the Npgsql case, since NodaTime instances are passed as-is to the ADO layer, code literal generation must be taken care of.
Most helpful comment
I wanted to provide an update on the project. As of v1.2.0 I was able to get the most common operations working for all of the types, ie
DATEADDandDATEPARTis supported forInstant,OffsetDateTime,LocalDateTime,LocalDate,LocalTime, andDuration. Thanks @bricelam for your work on a solution forHierarchyIdwhich I used as a resource for getting this all hooked up... and of course thanks to everyone on the EF Core team for a great codebase that allows for this plugin scenario without needing to modify any of the source!For more information see the repo here: https://github.com/StevenRasmussen/EFCore.SqlServer.NodaTime