Seeding database during migration fails when using newly added Enum

未关闭
#3,675 8 条评论 5 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
csharp, postgresql
领域
backend, databases

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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?

主要语言
C#
星标
1.8k
派生
268
平均合并
2 小时 35 分钟
30 天内合并 PR
136

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

npgsql/efcore.pg 的其他 Issue

查看 npgsql/efcore.pg 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。