I had found an answer in a different question where someone did not set up their many to many linking table correctly, in which I discovered that mine is also not set up correctly. I had a surrogate primary key, that I needed to replace with a composite primary key. I then needed to change out the Id I had as the Primary Key for the linking table prior to a ValueGeneratedOnAdd since it was used elsewhere. Afterwards, it seemed to accept that the Foreign key was not set until after the DB created it.
public class WorkOrderConfiguration : IEntityTypeConfiguration<WorkOrder>
{
public void Configure(EntityTypeBuilder<WorkOrder> builder)
{
builder
.HasMany(i => i.PurchaseRequests)
.WithMany(i => i.WorkOrders);
builder
.HasMany(wo => wo.PurchaseOrderLines)
.WithMany(s => s.RelatedWorkOrders)
.UsingEntity<WorkOrderPurchaseOrderLine>(
r => r
.HasOne(pol => pol.PurchaseOrderLine)
.WithMany(wopol => wopol.PurchaseOrderLineWorkOrders)
.HasForeignKey(fk => fk.PurchaseOrderLineId),
l => l
.HasOne(wo => wo.WorkOrder)
.WithMany(wopol => wopol.WorkOrderPurchaseOrderLines)
.HasForeignKey(fk => fk.WorkOrderId)
.HasKey(t => new { t.PurchaseOrderLineId, t.WorkOrderId }); //Needed to add this.
}
}
public class WorkOrderPurchaseOrderLineConfiguration : IEntityTypeConfiguration<WorkOrderPurchaseOrderLine>
{
public void Configure(EntityTypeBuilder<WorkOrderPurchaseOrderLine> builder)
{
builder.Property(b => b.Id)
.ValueGeneratedOnAdd();
}
}