我刚刚升级到实体框架核心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为此表创建一个额外的列?