79698786

Date: 2025-07-11 20:11:31
Score: 0.5
Natty:
Report link

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();
    }
}

Reasons:
  • Blacklisted phrase (0.5): I need
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Joe L