I have the following simplified classes using EF 5 code-first, where my class B has multiple FK's pointing back to class A.
public class A
{
public int Id {get;set;}
ICollection<B> Bs {get;set;}
}
public class B
{
public int Id {get;set;}
public int A1Id {get;set;}
public int A2Id {get;set;}
public int A3Id {get;set;}
[ForeignKey("A1Id")]
public A A1 {get;set;}
[ForeignKey("A2Id")]
public A A2 {get;set;}
[ForeignKey("A3Id")]
public A A3 {get;set;}
}
When I try to build my table I get this error: Introducing FOREIGN KEY constraint 'FK_dbo.B_dbo.A_A1Id' on table 'B' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors.
I tried modifying the cascade rules through a EntityTypeConfiguration:
HasOptional(x => x.A1)
.WithMany()
.HasForeignKey(p => p.A1Id)
.WillCascadeOnDelete(false);
Also tried this:
HasOptional(x => x.A1)
.WithMany()
.Map(y => y.MapKey("A1Id"))
.WillCascadeOnDelete(false);
Following this suggestions: Multiple foreign keys to same primary key table
But still got the same error. All my A1,A2 and A3 FK are optional.
Any ideas on how to get around this error?
Thanks!