Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
            });
like image 683
Felipe Antunes Avatar asked Sep 11 '25 10:09

Felipe Antunes


1 Answers

I use ValueGeneratedNever():

_ = builder.Property(x => x.Id).IsRequired().ValueGeneratedNever();

EF Core 5+ In the metadata of the function description is: Configures a property to never have a value generated when an instance of this entity type is saved.

like image 109
Martin4code Avatar answered Sep 16 '25 07:09

Martin4code