efcore icon indicating copy to clipboard operation
efcore copied to clipboard

EF 8 migration 'up' method being executed twice. Undocumented breaking change?

Open aaron-manning opened this issue 1 year ago • 6 comments

EF Core version: 8.0.2 Database provider: Npgsql 8.0.2 Target framework: net8.0 Operating system: Windows

I have migrated an application from EF 7 to EF 8 and noticed that the behavior of migrations seems to have changed.

namespace Api.Migrations
{
    /// <inheritdoc />
    public partial class testmigration : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            Console.WriteLine("migrating");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {

        }
    }
}

The code above will only log 'migrating' once on EF 7 (and EF6) but is being executed twice on EF8. This is causing issues with custom migrations that execute non-idempotent logic in their 'up' command (eg creating a transaction and performing operations on the DB using an EF context rather than using the migration builder to generate the sql)

I couldn't find this change at https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-8.0/breaking-changes Is this expected behavior?

aaron-manning avatar Mar 10 '24 11:03 aaron-manning

I can confirm, I came across the same error

mariuszkochanowski avatar Mar 15 '24 14:03 mariuszkochanowski

same issue on my side with

  • Npgsql 8.0.2
  • Npgsql.EntityFrameworkCore.PostgreSQL 8.0.2

It's work well with Microsoft.EntityFrameworkCore.SqlServer 8.0.3

Maleaume avatar Mar 21 '24 12:03 Maleaume

same issue on my side with

  • Npgsql 8.0.0
  • Npgsql.EntityFrameworkCore.PostgreSQL 8.0.0

souamaj avatar Mar 21 '24 19:03 souamaj

I noticed that issue was reported a while ago: https://github.com/npgsql/efcore.pg/issues/3040

mariuszkochanowski avatar Mar 26 '24 06:03 mariuszkochanowski

This is a PG-specific issue that was caused by https://github.com/npgsql/efcore.pg/issues/292 in 8.0 (trigger type reloading after new types are created in the database).

@maumar the cause here is the PG provider doing some special stuff, see https://github.com/npgsql/efcore.pg/issues/3040#issue-2055709863; it needs to go over the migrations that have been applied, in order to possibly reload types created by those applied migrations from the database. Hopefully we'd have a way for the provider to do that without building the migration twice.

roji avatar Mar 26 '24 07:03 roji

Same goes for EF Core version: 9.0.0-preview3.24172.4 Database provider: Npgsql 9.0.0-preview.3 Target framework: net9.0

PrincessLunaOfficial avatar May 08 '24 18:05 PrincessLunaOfficial

Sorry but it will be possible to have fix on target Framework net8.0 that is LTS ?

Maleaume avatar May 14 '24 08:05 Maleaume

@Maleaume that's very unlikely, EF 8.0 only receives critical, low-risk bugfixes, and doesn't seem to meet that bar.

roji avatar May 14 '24 08:05 roji

@roji, Thanks for the answer. I just have to know for tracability :)

Maleaume avatar May 15 '24 06:05 Maleaume