Adds native support to EntityFrameworkCore for SQL Server for the NodaTime types.
When modelling, usage of the following NodaTime types are supported:
time
in SQL Server which limits this type to < 24 hours)When querying, standard operators are supported as well as a range of additional mappings from NodaTime properties/function to their SQL Server equivalents.
All types and their methods have unit tests written to verify that the SQL is translated as expected. See individual tests for more information.
Note: To run the unit tests for the first time, you will need to uncomment the lines here. This ensures that the test DB is created locally.
To use, simply install the NuGet package:
Install-Package SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime
Note: Versioning follows the [major.minor] of EF Core so that it is easy to know which version to install. Ie, if you are using EF Core v5.x then you would install v5.x of this package. Build and revision numbers are not guaranteed to follow the same numbers.
Then call the UseNodaTime()
method as part of your SqlServer configuration during the UseSqlServer
method call:
using Microsoft.EntityFrameworkCore;
options.UseSqlServer("your DB Connection",
x => x.UseNodaTime());
Support for reverse engineering has been added starting in v5.0.2.
The SQL Server types map as follows:
smalldatetime
-> Instant
datetime
-> Instant
datetime2
-> Instant
date
-> LocalDate
time
-> LocalTime
datetimeoffset
-> OffsetDateTime
The SQL DATEADD
function is supported for the following types:
Note: Please add a using statement in order to use the extension methods:
using Microsoft.EntityFrameworkCore.SqlServer.NodaTime.Extensions;
using Microsoft.EntityFrameworkCore.SqlServer.NodaTime.Extensions;
// PlusYears
await this.Db.RaceResult
.Where(r => r.StartTime.PlusYears(1) >= Instant.FromUtc(2019, 7, 1, 1, 0))
.ToListAsync();
// Translates to:
// SELECT [r].[Id], [r].[EndTime], [r].[StartTime], [r].[StartTimeOffset]
// FROM [RaceResult] AS [r] WHERE DATEADD(year, CAST(1 AS int), [r].[StartTime]) >= '2019-07-01T01:00:00.0000000Z'
The SQL DATEPART
function is supported for the following types:
Note: Please add a using statement in order to use the extension methods:
using Microsoft.EntityFrameworkCore.SqlServer.NodaTime.Extensions;
using Microsoft.EntityFrameworkCore.SqlServer.NodaTime.Extensions;
// Compare the 'Year' DatePart
await this.Db.RaceResult
.Where(r => r.StartTime.Year() == 2019)
.ToListAsync();
// Translates to:
// SELECT [r].[Id], [r].[EndTime], [r].[StartTime], [r].[StartTimeOffset]
// FROM [RaceResult] AS [r] WHERE DATEPART(year, [r].[StartTime]) = 2019
The SQL DATEDIFF
function is supported for the following types:
Note: Please add a using statement in order to use the extension methods:
using Microsoft.EntityFrameworkCore.SqlServer.NodaTime.Extensions;
using Microsoft.EntityFrameworkCore.SqlServer.NodaTime.Extensions;
// DateDiff based on 'day'
DbFunctions dbFunctions = null;
await this.Db.Race
.Where(r => dbFunctions.DateDiffDay(r.Date, new LocalDate(2020, 1, 1)) >= 200)
.ToListAsync();
// Translates to:
// SELECT [r].[Id], [r].[Date], [r].[ScheduledDuration], [r].[ScheduledStart], [r].[ScheduledStartTime]
// FROM [Race] AS [r]
// WHERE DATEDIFF(DAY, [r].[Date], '2020-01-01') >= 200
The SQL DATEDIFF_BIG
function is supported for the following types:
Note: Please add a using statement in order to use the extension methods:
using Microsoft.EntityFrameworkCore.SqlServer.NodaTime.Extensions;
using Microsoft.EntityFrameworkCore.SqlServer.NodaTime.Extensions;
// DateDiffBig based on 'second'
DbFunctions dbFunctions = null;
await this.Db.RaceResult
.Where(r => dbFunctions.DateDiffBigSecond(r.StartTime, Instant.FromUtc(2019, 7, 1, 0, 0)) >= 100000)
.ToListAsync();
// Translates to:
// SELECT [r].[Id], [r].[EndTime], [r].[OffsetFromWinner], [r].[StartTime], [r].[StartTimeOffset]
// FROM [RaceResult] AS [r]
// WHERE DATEDIFF_BIG(SECOND, [r].[StartTime], '2019-07-01T00:00:00.0000000Z') >= CAST(100000 AS bigint)
LocalDateTime.Date
property - #35OffsetDateTime.Date
propertyArgumentNullException
would throw if you called UseInternalServiceProvider
- #27DateTime
queries failed in some instances - #25DateTime
queries failed in some instances - #25