繁体   English   中英

.NET Core 2.1中的更新迁移

[英]Update Migration in .NET Core 2.1

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

汽车模型

    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之后,出现该错误

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

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

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

干杯

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

在您的解决方案中删除“迁移文件夹”,打开包管理器控制台并输入:

Add-Migration initial

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM