question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

daterange is mapped incorrectly

See original GitHub issue

By default NpgsqlRange<DateTime> seems to map to tsrange. But I need it to be daterange.

Here is my model:

public class ServiceRate
{
    [Key]
    public int Id { get; set; }

    [Column(TypeName = "daterange")]
    public NpgsqlRange<DateTime> Period { get; set; }
}

During migration this maps correctly:

migrationBuilder.CreateTable(
    name: "ServiceRates",
    columns: table => new
    {
        Id = table.Column<int>(type: "integer", nullable: false)
            .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityAlwaysColumn),
        Period = table.Column<NpgsqlRange<DateTime>>(type: "daterange", nullable: false),
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_ServiceRates", x => x.Id);
    });

But the issues arise when using it like this:

dbContext.ServiceRates.Add(new ServiceRate
{
    Period = new NpgsqlTypes.NpgsqlRange<DateTime>(
        new DateTime(2021, 06, 01),
        true,
        false,
        default,
        false,
        true),
});

This maps to (note the time part):

Executing DbCommand [Parameters=[@p0='[01.06.2021 0:00:00,)' (DbType = Object)], CommandType='Text', CommandTimeout='30']
INSERT INTO "ServiceRates" ("Period")
VALUES (@p0)
RETURNING "Id";

And as a result:

Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
 ---> Npgsql.PostgresException (0x80004005): 42804: column "Period" is of type daterange but expression is of type tstzrange

So how do I properly map my column to daterange? First thought was to use User-defined ranges but I’m not sure how to map to tsrange in other entities.

BTW, docs mention that this should be mapped to daterange already.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
uhfathcommented, Oct 4, 2021

Thanks for the info. Yeah, I’m waiting for DateOnly to see how it works out. As for NodaTime, it’s being on my radar for already long time (pun intended), but just didn’t have a chance to look at it closely enough. Perhaps later, but thanks for the suggestion.

0reactions
rojicommented, Dec 17, 2022

@pokorsky I’m unable to reproduce this problem with the latest patch daily build (5.0.11-ci.20211004T154222) - with the code below, I can use NpgsqlRange<DateTime> just fine even without the explicit column type name:

Attempted repro
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

ctx.Blogs.Add(new Blog { Period = new(new(2020, 1, 1), new(2020, 1, 5)) });
await ctx.SaveChangesAsync();

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseNpgsql(@"Host=localhost;Username=test;Password=test")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();
}

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }

    // [Column(TypeName = "daterange")]
    public NpgsqlRange<DateTime> Period { get; set; }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Date Range Picker (JavaScript Component for Bootstrap) ...
Currently I have been working with code which I am not the author and the user spotted a very specific problem I couldn't...
Read more >
NpgsqlRange<DateTime>.Contains not working when ...
I explicitly map to daterange as it is saved as daterange in the database. Would using NodaTime solve this problem? Or..
Read more >
Is it possible to use a date range as map's key and then ...
The problem here is that the keys are ranges and I want to fetch based on a date that lies between the ranges....
Read more >
Date Range URLParams Problem - App Building
This can be reproduced by creating a dateRange component, mapping the urlparams as shown in the screenshot, setting a value, and then reloading...
Read more >
Date not correctly detected/mapped - Kibana
Hi all, first of all, I must say i am new in this elastic world. I hace a setup with rsyslog-logstash-Kibana (and also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found