Seeding database during migration fails when using newly added Enum

Đang mở
#3,675 8 bình luận 5 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
csharp, postgresql
Lĩnh vực
backend, databases

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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?

Ngôn ngữ chính
C#
Star
1.8k
Fork
268
Merge trung bình
2 giờ 35 phút
Pull request đã merge (30 ngày)
136

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của npgsql/efcore.pg

Tất cả issue của npgsql/efcore.pg

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.