I have two tables in an existing (MSSQL 2008 R2) database that are related by a link table.
The two tables are "Plan" and "Tips". The link table is "PlanTipLinks".
Plans can have many tips, and tips can be associated with multiple plans (i.e. it's a many-to-many relationship). In the application, I only care about the "Plan.Tips" relationship. I don't need the Tip.Plans inverse relationship.
The foreign key references in the link table cannot be null.
I'm using the following fluent API code to map this relationship:
modelBuilder.Entity<Plan>()
.HasMany(p => p.Tips)
.WithMany()
.Map("PlanTipLinks", (p, t) =>
new
{
PlanId = p.Id,
TipId = t.Id
});
This create the correct entries in the table. Problem is that, when I delete a plan, I get a foreign key exception on the PlanTipLinks table.
Presumably I need to tell it to cascade into the PlanTipLinks table when a plan is deleted, but I'm not sure how to do that. I don't seem to be able to call the WillCascadeOnDelete method using the HasMany/WithMany methods.
What am I missing here?