Skip to content

Drop custom collation in EF migration rollback #3183

Closed as duplicate
Closed as duplicate

Description

@Gendolph

Hello!

I try to add custom collation calling HasCollation(...) in OnModelCreating method of DbContext. Everething good, EF migration generate sql with CREATE COLLATION ....
But when I try to rollback this migration, there is no corresponding sql statement dropping my collation in generated sql script.

In results an error is occured when I try to apply modified migration next time (debug) - it can not create collation, because that already exists (and migration failed).

Is it necessary to add some extra configuration besides HasCollation to correctly rollback migration?

Using Npgsql.EntityFrameforkCore.PostgreSQL v8.0.4

Activity

  1. roji commented on Oct 27, 2024

    @roji
    Member

    Sorry for not answering this sooner.

    I cannot reproduce this... When a migration is generated that adds a collation, I can see a Down() method for dropping that collation:

    /// <inheritdoc />
    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterDatabase()
            .OldAnnotation("Npgsql:CollationDefinition:dummy", "POSIX,POSIX,libc,");
    }

    When I use dotnet ef database update <migration> to roll back to a migration state before the collation was added, I can see the collation being dropped:

    DROP COLLATION dummy;

    You'll have to provide a clear code sample and/or instructions for reproducing the error.

  2. Gendolph commented on Oct 28, 2024

    @Gendolph
    Author

    @roji Thank you for answer!
    I updated NuGet package and SDK to 8.0.10 and still have the issue.
    In very simple case with only one instruction in DbContext.OnModelCreating (.HasCollation(...)) I have empty Down method of migration.
    Please check attached code sample with "broken" migration (try to recreate it).
    CollationRollbackIssue.zip

    I used CLI cmd dotnet ef migrations add Initial

  3. roji commented on Oct 29, 2024

    @roji
    Member

    @Gendolph OK, I can indeed see this happening, but only if the collation is defined when creating the very first migration; if you add it in a later migration, everything seems to be working well. While that's technically an EF bug, the Down() method of the initial migration isn't very useful, as it's supposed to bring the database back to an empty state (you can basically just drop it instead).

    I've opened dotnet/efcore#35016 on the EF side to track fixing this, though given the low value it probably won't get much attention any time soon.

    I'll go ahead and close this for now, but please post back if I somehow misunderstood or there's a wider issue.

  4. added a commit that references this issue on Feb 1, 2026
    c18b826
  5. roji commented on Feb 1, 2026

    @roji
    Member

    Note: this is being fixed as part of #3604.

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