EF Core always create .Annotation(“SqlServer:Identity”, “1, 1”) on add-migration

主宰稳场 提交于 2021-02-11 06:16:33

问题


I'm having a problem with Migration EF Core.

To make the scenario clear, I'm assigning it to an existing database, which is intended to use migration for database control from now on. But I am having difficulties.

What I have done so far.

  1. I generated the scaffolding from the existing database.

  2. I added the migration, so it generated all the "Up" for database creation.

  3. In a clean database, ran update-database.

So far perfect, everything worked as expected. But from this step every time I generate a new migration he (the migration) insists on creating an Alter Column statement for all tables. For example:

  migrationBuilder.AlterColumn<int>(
            name: "rac_n_codigo",
            table: "tb_rac_raca",
            nullable: false,
            oldClrType: typeof(int),
            oldType: "int")
            .Annotation("SqlServer:Identity", "1, 1");

The creation table

  migrationBuilder.CreateTable(
            name: "tb_rac_raca",
            columns: table => new
            {
                rac_n_codigo = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1"),
                rac_c_nome = table.Column<string>(unicode: false, nullable: true),
                rac_d_alteracao = table.Column<DateTime>(type: "date", nullable: true),
                rac_c_usuario = table.Column<string>(unicode: false, nullable: true),
                rac_c_tipo = table.Column<string>(unicode: false, nullable: true),
                rac_d_modificacao = table.Column<DateTime>(type: "datetime", nullable: true),
                rac_c_unique = table.Column<Guid>(nullable: false, defaultValueSql: "(newid())"),
                rac_d_atualizado = table.Column<DateTime>(type: "datetime", nullable: false, defaultValueSql: "(getdate())"),
                rac_d_inclusao = table.Column<DateTime>(type: "datetime", nullable: false, defaultValueSql: "(getdate())")
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_tb_rac_raca", x => x.rac_n_codigo);
            });

回答1:


Assuming you are using Core 3.0 or Core 3.1!

Add package Microsoft.EntityFrameworkCore.Relational

Also, you might need to explicitly specify the identity column in the mapping.

builder.Property(o => o.Id).ValueGeneratedOnAdd().UseIdentityColumn();


来源:https://stackoverflow.com/questions/59673398/ef-core-always-create-annotationsqlserveridentity-1-1-on-add-migration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!