实体框架核心npgsql迁移alter sequence错误

时间:2019-06-20 15:15:30

标签: postgresql entity-framework-core ef-migrations

我有这样的迁移

migrationBuilder.AlterColumn<int>(
    name: "Id",
    schema: "{table-schema}",
    table: "{table-name}",
    nullable: false,
    oldClrType: typeof(int))
    .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn);

它会生成这样的sql

DO $$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '{migration-name}') THEN
        ALTER TABLE {table-schema}."{table-name}" ALTER COLUMN "Id" TYPE integer;
        ALTER TABLE {table-schema}."{table-name}" ALTER COLUMN "Id" SET NOT NULL;
        CREATE SEQUENCE "{table-schema}_Id_seq" AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE NO CYCLE;
    END IF;
END $$;

之后

DO $$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '{migration-name') THEN
        ALTER TABLE {table-schema}."{migration-name}" ALTER COLUMN "Id" SET DEFAULT (nextval('"{table-name}_Id_seq"'));
        ALTER SEQUENCE "{table-name}_Id_seq" OWNED BY "{table-name}"."Id";
    END IF;
END $$;

在迁移过程中出现以下错误:System.Exception: 42P01: relation "{table-name}" does not exist

这有什么问题吗?

当我手动运行sql时,在ALTER SEQUENCE行出现错误。 我正在使用npgsql provider 2.1.2,并在PostgreSQL 9.5和11中都尝试过。

0 个答案:

没有答案