我刚刚升级到了Entity Framework Core 2,现在出现了问题,即使它不在我的模型中且没有在任何其他地方定义,但仍存在一个额外的列并具有唯一键。
索引:
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会为这个表创建一个额外的列?