I just added an EF entity called PurchaseOrderProduct, which creates a many-to-many link between my PurchaseOrders and Products.
[PrimaryKey(nameof(PurchaseOrderId), nameof(ProductId))]
public class PurchaseOrderProduct
{
public int PurchaseOrderId { get; set; }
public PurchaseOrder PurchaseOrder { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
}
However, I don't quite understand the generated migration script. I want the primary key to be a compound key based on the PurchaseOrderId and ProductId columns. But why does it also create an additional IX_PurchaseOrderProducts_ProductId index on my ProductId column?
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "PurchaseOrderProducts",
columns: table => new
{
PurchaseOrderId = table.Column<int>(type: "int", nullable: false),
ProductId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_PurchaseOrderProducts", x => new { x.PurchaseOrderId, x.ProductId });
table.ForeignKey(
name: "FK_PurchaseOrderProducts_Products_ProductId",
column: x => x.ProductId,
principalTable: "Products",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_PurchaseOrderProducts_PurchaseOrders_PurchaseOrderId",
column: x => x.PurchaseOrderId,
principalTable: "PurchaseOrders",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_PurchaseOrderProducts_ProductId",
table: "PurchaseOrderProducts",
column: "ProductId");
}