使用单独的身份验证创建新的ASP.NET Core MVC项目(Core 2.2)时,初始迁移失败,并出现错误“要更改列的IDENTITY属性,需要删除并重新创建该列”。
遵循的过程如下:
dotnet new mvc -o <myapp> -auth Individual
创建项目dotnet ef database update
运行初始迁移以进行身份验证dotnet ef database update
此操作失败并显示 与下面的代码相关的“要更改列的IDENTITY属性,需要删除并重新创建该列”。
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
namespace CetaMonitor.Data.Migrations
{
public partial class InitialModel : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "UserNameIndex",
table: "AspNetUsers");
migrationBuilder.DropIndex(
name: "RoleNameIndex",
table: "AspNetRoles");
migrationBuilder.AlterColumn<int>(
name: "Id",
table: "AspNetUserClaims",
nullable: false,
oldClrType: typeof(int))
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
migrationBuilder.AlterColumn<int>(
name: "Id",
table: "AspNetRoleClaims",
nullable: false,
oldClrType: typeof(int))
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
migrationBuilder.CreateTable(
name: "Entity",
columns: table => new
{
ID = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
EntityName = table.Column<string>(nullable: true),
MainContact = table.Column<string>(nullable: true),
Address = table.Column<string>(nullable: true),
Province = table.Column<int>(nullable: false),
TelephoneNumbers = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Entity", x => x.ID);
});
migrationBuilder.CreateIndex(
name: "UserNameIndex",
table: "AspNetUsers",
column: "NormalizedUserName",
unique: true,
filter: "[NormalizedUserName] IS NOT NULL");
migrationBuilder.CreateIndex(
name: "RoleNameIndex",
table: "AspNetRoles",
column: "NormalizedName",
unique: true,
filter: "[NormalizedName] IS NOT NULL");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Entity");
migrationBuilder.DropIndex(
name: "UserNameIndex",
table: "AspNetUsers");
migrationBuilder.DropIndex(
name: "RoleNameIndex",
table: "AspNetRoles");
migrationBuilder.AlterColumn<int>(
name: "Id",
table: "AspNetUserClaims",
nullable: false,
oldClrType: typeof(int))
.OldAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
migrationBuilder.AlterColumn<int>(
name: "Id",
table: "AspNetRoleClaims",
nullable: false,
oldClrType: typeof(int))
.OldAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
migrationBuilder.CreateIndex(
name: "UserNameIndex",
table: "AspNetUsers",
column: "NormalizedUserName",
unique: true);
migrationBuilder.CreateIndex(
name: "RoleNameIndex",
table: "AspNetRoles",
column: "NormalizedName",
unique: true);
}
}
}
我希望新迁移可以正常运行,并且不依赖于Identity表。