【发布时间】: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 后出现该错误
【问题讨论】:
-
请注意,迁移尝试创建列:
UserId和UserID。这可能是问题的根源。可能你在CarModel中有错字,它应该是:[ForeignKey("UserId")]。尝试更改它并重新创建迁移。
标签: asp.net asp.net-core asp.net-core-2.0 asp.net-core-2.1