【发布时间】:2021-10-28 09:18:24
【问题描述】:
我正在尝试使用 fluent API 创建一个外键,但出现以下错误,老实说,我无法弄清楚如何减少集群的字节数或为什么它甚至那么高。
Introducing FOREIGN KEY constraint 'FK_CustomerOrderLine_CustomerOrder_OrderCode' on table 'CustomerOrderLine' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
Warning! The maximum key length for a clustered index is 900 bytes. The index 'PK_CustomerOrderLine' has maximum length of 904 bytes. For some combination of large values, the insert/update operation will fail.
CustomerOrder.cs:
public class CustomerOrder
{
public string OrderCode { get; set; }
public string Table { get; set; }
public OrderStatus Status { get; set; }
public decimal Total { get; set; }
public bool Paid { get; set; }
public List<CustomerOrderLine> CustomerOrderLines { get; set; }
}
客户订单行:
public class CustomerOrderLine
{
public string OrderCode { get; set; }
public int OrderLine { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
}
流畅的 API:
modelBuilder.Entity<CustomerOrder>(e =>
{
e.HasKey(p => p.OrderCode);
e.HasMany<CustomerOrderLine>()
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
e.Property(p => p.Total)
.HasPrecision(2);
});
/* Customer Order Line */
modelBuilder.Entity<CustomerOrderLine>(e =>
{
e.HasKey(p => new { p.OrderCode, p.OrderLine });
e.HasOne<CustomerOrder>()
.WithMany();
});
【问题讨论】:
标签: c# entity-framework entity-framework-core ef-fluent-api