【发布时间】:2017-09-26 23:23:27
【问题描述】:
我刚刚升级到 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 为什么要为这个表创建一个额外的列?
【问题讨论】:
标签: c# entity-framework ef-model-first