How to say Datetime - timestamp without time zone in EF Core 6.0
Asked Answered
C

1

17

I migrate an ASP.NET Core project from 3.1 to 6.0.

I have copied old migration and pasted it to our new version

Migration on EF Core 3.1 (old)

migrationBuilder.AddColumn<DateTime>(
                name: "CalendarStartDate",
                table: "DealOverview",
                nullable: false,
                defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

Migration in EF Core 6.0 (new)

migrationBuilder.AlterColumn<DateTime>(
                name: "StartDate",
                table: "DealOverview",
                type: "timestamp without time zone",
                nullable: false,
                oldClrType: typeof(DateTime),
                oldType: "timestamp with time zone");

The migration fails because this line

public DateTime StartDate { get; set; }

has changed.

I went from this package:

<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />

to this package:

<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.1" /> 
Castellatus answered 9/1, 2022 at 17:39 Comment(1)
"The migration fails" what error do you have?Suppliant
S
30

EF Core 6 Npgsql has introduced some breaking changes to timestamp handling logic. You can try to "revert" back to old behaviour by adding next line either to Startup or Program file:

AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);

But in general it is recommended to migrate to the new behaviour.

Suppliant answered 9/1, 2022 at 19:1 Comment(6)
Do you know how to use DateOnly type? When I do it creates this date in database: 292269055-12-02.Staggs
Is there a way pass this without using context?Bidentate
@Vivekkushwaha not, that I know of. But what is the problem with using AppContext?Suppliant
@Guru Stron I am upgrading aws lambdas from .net core 3.1 to .net 6, where the connections are created as per need. we are not using entity framework.Bidentate
@Vivekkushwaha AppContext is not related to EF, it's System.AppContextSuppliant
@Guru Stron thanks for pointing it out, I thought AppContext in the context of EF.Bidentate

© 2022 - 2024 — McMap. All rights reserved.