由于索引问题,无法创建 Fluent API 外键
Fluent API foreign key could not be created due to index issues
我正在尝试创建一个流畅的外键 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();
});
解决方法是将主键“Id”引入数据库生成的“CustomerOrderLine”,而不是复合键“OrderCode”和“OrderLine”。
您收到的错误是因为您两次配置 CustomerOrder
和 CustomerOrderLine
之间的关系。
modelBuilder.Entity<CustomerOrder>(e =>
{
e.HasKey(p => p.OrderCode);
e.HasMany<CustomerOrderLine>() // here the relationship is configured for the first time
.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>() // here it is configured a second time
.WithMany();
});
因此 EF 假定 CustomerOrder
和 CustomerOrderLine
之间存在两种不同的关系,并且您希望它们都级联(这是不可能的)。只需删除关系的第二个配置,错误就会消失。
您收到的警告可能是因为 OrderCode
是一个没有配置最大长度的字符串。添加适当的最大长度(例如 100 个字符)将可能解决问题。
我正在尝试创建一个流畅的外键 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();
});
解决方法是将主键“Id”引入数据库生成的“CustomerOrderLine”,而不是复合键“OrderCode”和“OrderLine”。
您收到的错误是因为您两次配置 CustomerOrder
和 CustomerOrderLine
之间的关系。
modelBuilder.Entity<CustomerOrder>(e =>
{
e.HasKey(p => p.OrderCode);
e.HasMany<CustomerOrderLine>() // here the relationship is configured for the first time
.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>() // here it is configured a second time
.WithMany();
});
因此 EF 假定 CustomerOrder
和 CustomerOrderLine
之间存在两种不同的关系,并且您希望它们都级联(这是不可能的)。只需删除关系的第二个配置,错误就会消失。
您收到的警告可能是因为 OrderCode
是一个没有配置最大长度的字符串。添加适当的最大长度(例如 100 个字符)将可能解决问题。