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

.net-core ef-core-2.2 ef-migrations entity-framework-core


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:

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

The creation table

            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/13/2020 4:54:50 PM

Accepted Answer

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();
3/23/2020 2:23:48 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow