我剛剛升級到實體框架核心2,現在我得到一個額外的列存在問題,並擁有一個唯一的鍵,即使它不在我的模型,它是沒有在其他地方定義。實體框架核心 - 外鍵1(額外的外鍵列)
指數:
migrationBuilder.CreateTable(
name: "Vouchers",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
Code = table.Column<Guid>(nullable: false),
IsClaimed = table.Column<bool>(nullable: false),
LastModified = table.Column<DateTime>(nullable: false),
NumberOfUnits = table.Column<int>(nullable: false),
TransactionId = table.Column<int>(nullable: false),
TransactionId1 = table.Column<int>(nullable: true) // why is this here?
},
constraints: table =>
{
table.PrimaryKey("PK_Vouchers", x => x.Id);
table.ForeignKey(
name: "FK_Vouchers_Transactions_TransactionId1",
column: x => x.TransactionId1,
principalTable: "Transactions",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
TransactionId1不在模型:
public class Voucher : IAutoLastModified
{
public int Id { get; set; }
public DateTime LastModified { get; set; }
public int TransactionId { get; set; }
public Transaction Transaction { get; set; }
public int NumberOfUnits { get; set; }
public Guid Code { get; set; }
public bool IsClaimed { get; set; }
}
我是否定義了外鍵錯誤?
modelBuilder.Entity<Voucher>().HasOne(x => x.Transaction).WithOne(x => x.Voucher).IsRequired(false);
我的應用程序失敗,因爲TransactionId1總是會爲空,並且具有我無法刪除的唯一約束。
爲什麼EF爲此表創建一個額外的列?