Skip to content

Is it possible to convert timestamptz to DateTime instead of using DateTimeOffset everywhere? #201

Description

@kolev9605

We are doing a PostgreSQL migration to our codebase. One of our services is not yet migrated to .NET 8 but it is using the same DB as the other services which are already migrated to .NET 8.

The problem we are facing is the following:

Let's take a look first at the .NET 8 setup:

Service 1, .NET 8, EF Core 8.

Versions:

...
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
...

DB Context & entity:

public class SomeEntity
{
    public int Id { get; set; }

    public DateTime SomeDate { get; set; }
}

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options)
        : base(options)
    {
    }

    public DbSet<SomeEntity> SomeEntities { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.ApplyConfigurationsFromAssembly(typeof(MyDbContext).Assembly);

        modelBuilder.Entity<SomeEntity>()
            .Property(x => x.SomeDate)
            .HasColumnType("timestamptz");
    }
}

When I generate a migration, it works fine and the type chosen is timestamp with time zone. From that point onward, read/write operations work fine, and I am able to fetch a UTC timestamp into a DateTime property.

In .NET Framework, though, it is not like that.

Service 2, .NET Framework 4.8, EF6.

Versions:

...
<package id="EntityFramework" version="6.4.0" targetFramework="net48" />
<package id="EntityFramework6.Npgsql" version="6.4.3" targetFramework="net48" />
<package id="Npgsql" version="4.1.3" targetFramework="net48" />
...

DB Context & Entity:

public class SomeEntity
{
    public int Id { get; set; }

    public DateTime SomeDate { get; set; }
}

public class MyDbContext : DbContext
{
    public MyDbContext()
        : base("MyConnection")
    {
    }

    public virtual DbSet<SomeEntity> SomeEntities { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.HasDefaultSchema("public");

        modelBuilder.Entity<SomeEntity>()
            .Property(x => x.SomeDate)
            .HasColumnType("timestamptz");
    }
}

If I try to add a migration here, though, I get the following error:

Schema specified is not valid. Errors: 
(8,12) : error 2019: Member Mapping specified is not valid. The type 'Edm.DateTime[Nullable=False,DefaultValue=,Precision=]' of member 'SomeDate' in type 'NpgsqlTesting.Web.Data.SomeEntity' is not compatible with 'Npgsql.timestamptz[Nullable=False,DefaultValue=,Precision=7]' of member 'SomeDate' in type 'CodeFirstDatabaseSchema.SomeEntity'.

It works fine if I change the type of SomeDate to DateTimeOffset.

I understand (kind of) why it works like that reading the breaking changes with version 6.0 of Npgsql, but is there something we can do to force EF to map the timestamptz columns to DateTime properties?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions