11

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 want from this package:

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

to this package:

<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.1" /> 
1
  • 1
    "The migration fails" what error do you have?
    – Guru Stron
    Jan 9, 2022 at 19:02

1 Answer 1

23

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.

6
  • Do you know how to use DateOnly type? When I do it creates this date in database: 292269055-12-02.
    – cikatomo
    Apr 26, 2022 at 22:41
  • Is there a way pass this without using context? Sep 1, 2022 at 6:49
  • @Vivekkushwaha not, that I know of. But what is the problem with using AppContext?
    – Guru Stron
    Sep 1, 2022 at 6:51
  • @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. Sep 1, 2022 at 9:44
  • @Vivekkushwaha AppContext is not related to EF, it's System.AppContext
    – Guru Stron
    Sep 1, 2022 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.