CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime
10.4.0
Prefix Reserved
dotnet add package CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime --version 10.4.0
NuGet\Install-Package CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime -Version 10.4.0
<PackageReference Include="CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime" Version="10.4.0" />
<PackageVersion Include="CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime" Version="10.4.0" />
<PackageReference Include="CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime" />
paket add CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime --version 10.4.0
#r "nuget: CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime, 10.4.0"
#:package CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime@10.4.0
#addin nuget:?package=CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime&version=10.4.0
#tool nuget:?package=CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime&version=10.4.0
CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime
This package adds NodaTime to the
CodoMetis.ValueRanges EF Core package.
You store the range and value set types of
CodoMetis.ValueRanges.NodaTime in
PostgreSQL range, multirange and array columns, and query them with LINQ. Under the hood it goes
through NpgsqlRange<T> and native arrays, using Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime.
It needs PostgreSQL 16 or later.
dotnet add package CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime
options.UseNpgsql(connectionString, npgsql => npgsql.UseValueRangesNodaTime());
UseValueRangesNodaTime() turns on two things for you, so you don't call either one yourself:
UseNodaTime(), the Npgsql NodaTime plugin. It maps the element typesLocalDate,LocalDateTimeandInstant.UseValueRanges(), the base package. Your BCL range and set types keep working in the same model.
It registers the range types and the five value set types. You don't configure anything per property.
Which column each type gets
Ranges and multiranges
| Property type | Column type |
|---|---|
LocalDateRange |
daterange |
RangeSet<LocalDateRange, LocalDate> |
datemultirange |
LocalDateTimeRange |
tsrange |
RangeSet<LocalDateTimeRange, LocalDateTime> |
tsmultirange |
InstantRange |
tstzrange |
RangeSet<InstantRange, Instant> |
tstzmultirange |
YearMonthRange |
daterange (month-aligned) |
RangeSet<YearMonthRange, YearMonth> |
datemultirange (month-aligned) |
Value sets
The five value set types arrived in v6:
| Property type | Column type |
|---|---|
LocalDateSet |
date[] |
LocalDateTimeSet |
timestamp without time zone[] |
InstantSet |
timestamp with time zone[] |
LocalTimeSet |
time without time zone[] |
YearMonthSet |
date[] (month-aligned) |
Plain LocalDate[] and List<Instant> properties keep Npgsql's own array mapping, and they can
sit in the same model.
Good to know:
- Scaffolding doesn't produce these types.
dotnet ef dbcontext scaffoldstill produces plain arrays, as with the base package. Change the property types yourself after scaffolding. Using a set type is a decision about your model. - Your own
NpgsqlDataSourceneedsUseNodaTime()too. When your application builds its own data source instead of letting EF Core create one, callUseNodaTime()on that data source builder as well.Npgsql.EntityFrameworkCore.PostgreSQL.NodaTimedocuments the same requirement. - A lookup by store type name alone finds Npgsql's own type. With the Npgsql NodaTime plugin
active, resolving a mapping by name only, such as a scaffolding-style
FindMapping("daterange"), returns Npgsql's ownDateIntervalmapping. The range types always resolve by CLR type, which is unambiguous.
Query ranges
The full range algebra runs in the database, exactly as in the base package:
- The operators:
@>,&&,<<,-|-, and the rest. - The bound accessors:
lower,upper,lower_inc,upper_inc. Merge, asrange_merge.GapandGaps, as multirange differences.- The aggregates
RangeAggandRangeIntersectAgg. Intersect,UnionandExcept.- Multirange operations, including
==equality. - The factories
CreateFinite,CreateUnboundedStartandCreateUnboundedEnd, as range constructor calls with a guard.
var day = new LocalDate(2024, 6, 15);
reservations.Where(r => r.Period.Contains(day)); // r."Period" @> DATE '2024-06-15'
reservations.OrderBy(r => r.Period.LowerBound()); // ORDER BY lower(r."Period")
reservations.Where(r => r.Window.Overlaps(other)); // r."Window" && @other
reservations.GroupBy(r => r.CustomerId)
.Select(g => g.Select(r => r.Period).RangeAgg()); // range_agg(r."Period")
Good to know:
- Discrete ranges come back exactly as in memory. The package makes up for PostgreSQL's
half-open
daterangeexactly as it does forDateRange.LocalDateRange.UpperBound()runs asupper(x) - 1. So results from the database always equal the results in memory, checked against a live PostgreSQL.
Query value sets
The value set methods run as the array operators @>, &&, <@, cardinality and
array_remove, and Union, Add, Intersect and Except as one subquery that deduplicates and
sorts, as memory does:
reservations.Where(r => r.Holidays.Contains(day)); // r."Holidays" @> ARRAY['2024-06-15']::date[]
reservations.Where(r => r.Holidays.Overlaps(closures)); // r."Holidays" && @closures
reservations.Where(r => r.Holidays.IsSubsetOf(allowed)); // r."Holidays" <@ @allowed
reservations.Where(r => r.BillingMonths.Count > 2); // cardinality(r."BillingMonths") > 2
reservations.Where(r => r.Holidays.Remove(day).IsEmpty); // cardinality(array_remove(…)) = 0
reservations.Where(r => r.Holidays.Union(closures) == all); // ARRAY((SELECT DISTINCT ON (vr0.x) vr0.x FROM unnest(…) …)) = @all
Contains always runs as @>, never as = ANY, so a plain GIN index on the column serves it.
Good to know:
- All five NodaTime sets get the same translations. That includes their versions with your own
element type, and
YearMonthSet, whose months sort as their first days. The EF Core guide lists what runs where in What runs where. - Equal values keep the first copy, as in memory. For a
Union, the set you call it on comes first. Removeis safe everywhere.array_removeleaves the array sorted and deduplicated.
What gets stored
- The value you write is the value stored. The base package reinterprets
DateTimeKindfortsrangeand normalizes offsets to UTC fortstzrange. Neither happens here.LocalDateTimeis wall-clock time by construction, andInstantis an instant by construction. - PostgreSQL stores microseconds. NodaTime carries nanoseconds, so you lose any precision below the microsecond at the database boundary.
Instant.MinValueandMaxValueare stored as-infinityandinfinity. That is Npgsql's default rule. Such a bound is a finite bound that happens to be infinite. It stays different from an unbounded side, soupper_infstaysfalse.
Months
YearMonthRange(v5) is stored as a month-aligneddaterange.[2025-01, 2025-03]becomes[2025-01-01, 2025-04-01). So you need no custom database type, and every operator runs in the database.- Its bounds go through first-of-month dates.
Contains(yearMonth)runs as@> DATE 'yyyy-MM-01'.UpperBound()runs asupper(x) - 1, which lands on the last day of the end month and reads back as that month. - Reading a
YearMonthRangechecks month alignment. A partial-monthdaterangethrows instead of shifting quietly. - The factories can't build a
YearMonthRangein SQL from column values. Months are coarser than thedateelement type. Constant and parameter ranges work as usual. YearMonthSet(v6) stores first-of-month dates in adate[], exactly asYearMonthRangedoes for its bounds. Reading it checks alignment and throws on a partial-month date, rather than shifting it quietly to its month.
Other calendars
The same calendar rules apply in memory, as the
NodaTime package README
describes. In a query, they also reach a value you pass on its own, such as the argument to
Contains:
LocalDateSetandLocalDateTimeSetconvert it to ISO before it is bound. They hold ISO elements. SoHolidays.Contains(copticDate)queries the ISO date that the Coptic date denotes. It doesn't read the Coptic year, month and day as if they were ISO.YearMonthSetandYearMonthRangereject a non-ISO value outright. A year-month in another calendar has no lossless ISO equivalent.- The versions over your own element type reject a non-ISO element (
ArgumentException).LocalDateSet<T>,LocalDateTimeSet<T>andYearMonthSet<T>can't convert your element. So a non-ISO one throws when you pass it on its own, as a parameter, an inlined constant or inside a JSON document. That matches what the set does when you create it. Before 9.0, such an element was bound as the ISO date its own calendar's fields spell.
Documentation
The EF Core guide covers both packages, with every translation and the SQL it generates. You can read every guide, with an API reference, on the documentation site. The walkthrough follows one booking through a minimal API and PostgreSQL, and shows what changes at each step.
License
MIT — see LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- CodoMetis.ValueRanges.EFCore.PostgreSQL (>= 10.4.0)
- CodoMetis.ValueRanges.NodaTime (>= 10.4.0)
- Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime (>= 10.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 10.4.0 | 345 | 10/26/2026 |
| 10.3.0 | 581 | 10/25/2026 |
| 10.2.0 | 864 | 10/25/2026 |
| 10.1.0 | 757 | 10/19/2026 |
| 10.0.0 | 331 | 10/19/2026 |
| 9.1.0 | 1,595 | 10/17/2026 |
| 9.0.0 | 257 | 10/11/2026 |
| 8.0.0 | 406 | 8/17/2026 |
| 7.0.0 | 197 | 8/17/2026 |
| 6.3.0 | 152 | 8/16/2026 |
| 6.2.1 | 166 | 8/16/2026 |
| 6.2.0 | 231 | 8/15/2026 |
| 6.1.0 | 132 | 8/14/2026 |
| 6.0.0 | 138 | 8/14/2026 |
| 5.0.0 | 138 | 8/13/2026 |
| 4.1.0 | 137 | 8/12/2026 |
Some calls answer differently: Changed answers in 10.4.0 (https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/migration.md#changed-answers-in-1040).
Changed answers
- EF Core: on a LocalDateRange and its multirange, UpperBound() and Clamp read an exclusive infinity as 9999-12-30 in a query, as the row loads, where they returned 9999-12-31, and LowerBoundInclusive() is true at an exclusive -infinity. LowerBound() there keeps LocalDate.MinIsoValue, because memory's -9998-01-02 isn't a PostgreSQL date (#502 (https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/issues/502), Migration (https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/migration.md#changed-answers-in-1040)).
- EF Core: on a YearMonthRange and its multirange, UpperBound() and Clamp compare with a month in a query as in memory. They read the last day of the month, so on [2026-01,2026-01] UpperBound() == 2026-01 was false and UpperBound() > 2026-01 was true (#511 (https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/issues/511), Migration (https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/migration.md#changed-answers-in-1040)).
Added
- EF Core: Union, Add, Intersect and Except on the NodaTime value sets, their versions with your own element type and YearMonthSet run as SQL. They give the deduplicated, sorted set memory gives, so a query can compare, count, group by and store the result (What runs where (https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/efcore.md#what-runs-where)).
- EF Core: Count on a RangeSet of the NodaTime ranges and of YearMonthRange runs as SQL, so a query can filter and sort by how many separate ranges a multirange holds (What runs where (https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/efcore.md#what-runs-where)).
- EF Core: Clamp runs as SQL on LocalDateRange, LocalDateTimeRange, InstantRange and YearMonthRange, so a query can filter and sort by the clamped value. It answers as memory does, at the element types' extremes too (What runs where (https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/efcore.md#what-runs-where)).
Full changelog: https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/v10.4.0/src/CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime/CHANGELOG.md