Joshit
Joshit

Reputation: 1335

Compare NodaTime-Periods in EF Core

I'm building an application, using .NET (8), EF Core (8) and Postgres (16). I use nodaTime for every date or time related task and NodaTime.Periods are stored in pg as interval.

I configure them explicitly as interval via IEntityTypeConfiguration<MyType> configurations.

Ef is configured like this:

services.AddDbContext<MyCtx>(opt => opt.UseNpgsql(conn, npgOpt => { npgOpt.UseNodaTime() }));

So far so good ... Now I want to fetch records, Where(e=>e.Period <= givenPeriod); which gives a compiler error, as Periods cannot be compared directly. If I want to compare Periods in memory, I convert them to Duration (except they're to big) and compare Durations. That however could not be translated to valid sql.

Since this call is relatively simple at database level (SELECT * FROM "Entity" where "Period" > interval '8 hours') I wonder if there is a solution to query this with linq.

ctx.Entities.FromRawSql("SELECT * FROM ""Entities"" where ""Period"" > interval '8 hours'").ToListAsync();

Doesn't work either while the query returns expected results when executed directly - any ideas how to solve this preferably without raw sql?

One more thing... I found this github-comment where two different configurations regarding nodaTime for EF and npgsql are mentioned. Do I need to update my db-context configuration to solve this issue?

Upvotes: 0

Views: 36

Answers (0)

Related Questions