Seeding database during migration fails when using newly added Enum

Aperta
#3,675 8 commenti 5 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
csharp, postgresql
Ambito
backend, databases

Direzione di ricerca

Start with the migration flow in EFCore.Relational/Migrations/Internal/Migrator.cs, especially the seeding and ReloadTypes order linked in the issue. Reproduce with the EF Core 9 migration bundle, newly added PostgreSQL enum, and seeder described in the issue; compare the failure before and after calling ReloadTypes. Done means seeding during migration can use the new enum without either reported exception.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

I have a migration in EF Core 9 (package: Npgsql.EntityFrameworkCore.PostgreSQL 9.0.4) adding new enum type:

   migrationBuilder.AlterDatabase()
      .Annotation("Npgsql:Enum:event_stack_event_status", "closed,new,taken,unknown")

The enum is registered like so:

       services.AddDbContext<DatabaseContext>(options =>
       {
           options.UseNpgsql(
               CreateConnString(variables.DatabaseName),
               npsqlOptions =>
               {
                   npsqlOptions.UseQuerySplittingBehavior(QuerySplittingBehavior.SingleQuery);
                   npsqlOptions.MapEnum<EventStackEventStatus>();
               });
           options.UseDataSeeding();
           options.ConfigureWarnings(c => c.Log((RelationalEventId.CommandExecuted, LogLevel.Debug)));
       }, ServiceLifetime.Transient);

The UseDataSeeding is my extension simply adding seeder implementation:

    public static void UseDataSeeding(this DbContextOptionsBuilder options)
    {
        options.UseSeeding(
            (ctx, storeManagementPerformed) => SeedAsync(ctx, storeManagementPerformed).Wait());

        options.UseAsyncSeeding(SeedAsync);
    }

Migration is applied via migration bundle. During the seed process, where the enum is used, I'm getting error:

System.NotSupportedException: The data type name 'event_stack_event_status' isn't present in your database. You may need to install an extension or upgrade to a newer version.

As far as I know this is an issue with type cache which doesn't contain new enum mapping. I tried to call ReloadTypes in data seeder on current database connection from database context but it changed error to:

System.InvalidCastException: Writing values of 'EventStackEventStatus' is not supported for parameters having DataTypeName 'public.event_stack_event_status'

I'm aware of this issue: #292 and PR that solving it #2951, but it's not applying here because seeder is called during Migrate method before ReloadTypes is called: https://github.com/dotnet/efcore/blob/main/src/EFCore.Relational/Migrations/Internal/Migrator.cs#L192

The only workaround I see right now is to move data seeding to application start. Is there a way to solve it so I can still use data seeding during migration?

Lingua principale
C#
Stelle
1.8k
Fork
268
Merge medio
2h 35m
PR unite (30g)
136

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di npgsql/efcore.pg

Tutte le issue di npgsql/efcore.pg

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.