简体   繁体   English

.NET Core 2.1中的更新迁移

[英]Update Migration in .NET Core 2.1

While update-migration I get the error "To change the IDENTITY property of a column, the column needs to be dropped and recreated." 在update-migration时,出现错误“要更改列的IDENTITY属性,需要删除并重新创建该列”。 wite .NET Core 2.1 在.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; }

Migration that I get 我得到的迁移

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

I try to create Car table in my database, but after I use update-migration I get that error 我尝试在数据库中创建Car表,但是在使用update-migration之后,出现该错误

After some research, I see some link to solve this issue. 经过研究,我看到一些链接可以解决此问题。 You can try this, if not please let me know and I would love to support you. 您可以尝试一下,如果没有,请告诉我,我很乐意为您提供支持。

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

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

Cheers 干杯

I have solved this problem by creating the new project with Dot Net core 2.0 and it works fine. 我已经通过使用Dot Net core 2.0创建新项目解决了该问题,并且工作正常。 I think the problem is because I try to upgrade from 2.0 to be 2.1.1 after I upgrade everything is broke. 我认为问题是因为升级失败后,我尝试从2.0升级到2.1.1。 Thank you for all answer 谢谢大家的回答

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

Add-Migration initial

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

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