Skip to content

Enum mapping with v9 and ordering #3390

@bmarkovic17

Description

@bmarkovic17

Hello,

I have a question about the new enum mapping with which we are having some issues while doing our migration from v8 to v9. We are migrating a project that has 3 enums mapped I would say in the standard way:

  1. On our NpgsqlDataSource we used MapEnum<T> to map each enum
  2. In our OnModelCreating method we used HasPostgresEnum<T> to register each of the enums

After updating the nuget packages and dotnet-ef tool, we created a new migration that wanted to revert our enum based table columns back to integers. We saw in the documentation that enum mapping is changing considerably with v9, so we removed invocations of the HasPostgresEnum<T> from our OnModelCreating method and added MapEnum<T> calls to UseNpgsql's npgsqlOptionAction delegate (both on our IDesignTimeDbContextFactory implementation and AddDbContextFactory<T>'s optionsAction). Unfortunately, that didn't work out as the migration created with this configuration resulted in recreating the same DB enums but with different sort order (the order before was the same as in our C# enum, but with v9 it is alphabetically)

...
    .Annotation("Npgsql:Enum:offering.market_status", "active,off,resulted,suspended")
    .OldAnnotation("Npgsql:Enum:offering.market_status", "off,suspended,active,resulted")
...

After further analysis and troubleshooting we noticed that if we also add back the HasPostgresEnum<T> part to our OnModelCreating method we get it to work as it was, i.e. new migrations aren't changing anything about our existing mapped enums. I'm not sure if we are doing something wrong or this is supposed to work like this, but with v9 we now have to keep our enum mappings in 4 places, while before we had to keep it in 2, which was also not ideal.

Thanks.

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