问题
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.
I generated the scaffolding from the existing database.
I added the migration, so it generated all the "Up" for database creation.
- 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