0

Why is this so #$*% hard? Shouldn't be.

Two tables Orders & Shippers: Orders.ship_via, an int is the FK to Shippers.ShipperId Shippers.ShipperId is the PK on Shippers

My entities:

public class Order : Entity
{
  public int OrderId { get; set; }
  public int ShipVia { get; set; }
  //More properties
  public virtual Shipper Shipper { get; set; }
}

public class Shipper : Entity
{
  public int ShipperId { get; set; }
  //More properties
}

When I run this, EF tries to naturally populate Order.Shipper using Order.ShipperId which doesn't exist.

I don't want to use annotations. How do I create the fluent map?

(Note: my test environment uses Northwind if you want to run tests).

Rap
  • 6,851
  • 3
  • 50
  • 88

1 Answers1

2

Try something like this:

modelBuilder.Entity<Order>()
   .HasRequired(o => o.Shipper) // Or .HasOptional if it's not required
   .WithMany() // No reverse navigation property
   .HasForeignKey(o => o.ShipVia)
   .WillCascadeOnDelete(true);
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151