.NET Core 2.1中的更新迁移

时间:2018-06-25 19:04:03

标签: asp.net asp.net-core asp.net-core-2.0 asp.net-core-2.1

在进行update-migration时,出现错误“要更改列的IDENTITY属性,需要删除并重新创建该列”。 Wite .NET Core 2.1

CarModel

    public int Id { get; set; }
    public string VIN { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public string Style { get; set; }
    public int Year { get; set; }
    public double Miles { get; set; }
    public string Color { get; set; }

    public string UserId { get; set; }
    [ForeignKey("UserID")]
    public virtual ApplicationUser ApplicationUser { get; set; }

我得到的移民

public partial class AddCarToDb : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Cars",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                VIN = table.Column<string>(nullable: true),
                Make = table.Column<string>(nullable: true),
                Model = table.Column<string>(nullable: true),
                Style = table.Column<string>(nullable: true),
                Year = table.Column<int>(nullable: false),
                Miles = table.Column<double>(nullable: false),
                Color = table.Column<string>(nullable: true),
                UserId = table.Column<string>(nullable: true),
                UserID = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Cars", x => x.Id);
                table.ForeignKey(
                    name: "FK_Cars_AspNetUsers_UserID",
                    column: x => x.UserID,
                    principalTable: "AspNetUsers",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateIndex(
            name: "IX_Cars_UserID",
            table: "Cars",
            column: "UserID");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable(
            name: "Cars");
    }
}

我尝试在数据库中创建Car表,但是在使用update-migration后,出现该错误

3 个答案:

答案 0 :(得分:0)

经过研究,我看到一些链接可以解决此问题。 您可以尝试一下,如果没有,请告诉我,我很乐意为您提供支持。

https://thisworksonmymachine.com/2017/02/13/ef-core-the-setup-part-4/

https://github.com/aspnet/EntityFrameworkCore/issues/329

欢呼

答案 1 :(得分:0)

我已经通过使用Dot Net core 2.0创建新项目解决了该问题,并且工作正常。我认为问题是因为升级失败后,我尝试从2.0升级到2.1.1。谢谢你的回答

答案 2 :(得分:0)

在解决方案中删除迁移文件夹,打开包管理器控制台 并写:

Add-Migration initial