【问题标题】:Update Migration in .NET Core 2.1.NET Core 2.1 中的更新迁移
【发布时间】:2018-06-25 19:04:02
【问题描述】:

更新迁移时,我收到错误消息“要更改列的 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 后出现该错误

【问题讨论】:

  • 请注意,迁移尝试创建列:UserIdUserID。这可能是问题的根源。可能你在CarModel 中有错字,它应该是:[ForeignKey("UserId")]。尝试更改它并重新创建迁移。

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


【解决方案1】:

经过一番研究,我看到了一些解决此问题的链接。 你可以试试这个,如果没有,请告诉我,我很乐意支持你。

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

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

干杯

【讨论】:

    【解决方案2】:

    我已经通过使用 Dot Net core 2.0 创建新项目解决了这个问题,它工作正常。我认为问题是因为我在升级后尝试从 2.0 升级到 2.1.1 一切都坏了。谢谢大家的回答

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 2020-08-03
      • 2018-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多