I wish to reference the OrderAddress
model twice in my Order
model; once as a ShippingAddress
and once as a Billing
You need to change your OrderAddress
entity and your Fluent API mappings to the following:
OrderAddress:
public class OrderAddress : BaseModel
{
...
public virtual ICollection BillingOrders { get; set; }
public virtual ICollection ShippingOrders { get; set; }
...
}
Fluent API:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity()
.HasRequired(m => m.ShippingAddress)
.WithMany(t => t.ShippingOrders)
.HasForeignKey(m => m.ShippingAddressId)
.WillCascadeOnDelete(false);
modelBuilder.Entity()
.HasRequired(m => m.BillingAddress)
.WithMany(t => t.BillingOrders)
.HasForeignKey(m => m.BillingAddressId)
.WillCascadeOnDelete(false);
}
Check this SO post for more, it is about the same problem as yours.