带有身份初始迁移的ASP.NET Core MVC因身份表的ALTER COLUMN而失败

时间:2019-05-24 11:20:15

标签: entity-framework asp.net-core asp.net-identity

使用单独的身份验证创建新的ASP.NET Core MVC项目(Core 2.2)时,初始迁移失败,并出现错误“要更改列的IDENTITY属性,需要删除并重新创建该列”。

遵循的过程如下:

  • 使用dotnet new mvc -o <myapp> -auth Individual创建项目
  • 修改连接字符串并更改为本地运行的SQlServer express
  • 修改startup.cs以使用SQLServer代替SQLite
  • 成功使用dotnet ef database update运行初始迁移以进行身份​​验证
  • 添加新的应用模型
  • 修改ApplicationDbContext以包含用于应用程序模型的新DbSet。
  • 创建新的迁移`dotnet ef迁移添加
  • 使用dotnet ef database update
  • 更新数据库

此操作失败并显示 与下面的代码相关的“要更改列的IDENTITY属性,需要删除并重新创建该列”。

问题:

  1. 如果未进行任何更改,为什么EF为何需要更改现有表的列。
  2. 该如何解决。

假设:

  1. 我使用相同的ApplicationDbContext,因为我希望Identity表与应用程序数据位于同一数据库中。
  2. 是否建议使用2个指向同一数据库的DbContext?
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表。

0 个答案:

没有答案