EF Core始终在添加迁移时创建.Annotation("SqlServer:Identity","1,1") [英] EF Core always create .Annotation("SqlServer:Identity", "1, 1") on add-migration

查看:51
本文介绍了EF Core始终在添加迁移时创建.Annotation("SqlServer:Identity","1,1")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在迁移EF 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.

到目前为止我所做的.

  1. 我从现有数据库中生成了脚手架.

  1. I generated the scaffolding from the existing database.

我添加了迁移,因此它为数据库创建生成了所有的"Up".

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

到目前为止,一切都很正常.但是从这个步骤开始,每次我生成新迁移时,他(迁移)都坚持要为所有表创建一个Alter Column语句.例如:

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");

创建表

  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);
            });

推荐答案

假设您使用的是Core 3.0或Core 3.1!

Assuming you are using Core 3.0 or Core 3.1!

添加软件包Microsoft.EntityFrameworkCore.Relational

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();

这篇关于EF Core始终在添加迁移时创建.Annotation("SqlServer:Identity","1,1")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆