I'm developing a DataBase on MS SQL Server with EF 6 - Code First I have schema like so: Ads|Messages|Attachments
where
Ads has many Messages Ads has many Attachments
Messages has many Attachments
The Attachments table has 2 FKs one to Ads and one to Messages. The Messages table has 1FK to Ads.
I want to create Cascading Rule Where when i delete Ad it deletes all messages for this Ad with their Attachments(Message Attachments), and the Ad's Attachments, but i'm failing so far.
I get
Introducing FOREIGN KEY constraint 'FK_dbo.Attachments_dbo.Messages_MessageId' on table 'Attachments' 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 or index. See previous errors.
The ModelBuilder Code is the Following
modelBuilder.Entity<Attachments>()
.HasOptional(a => a.Advertisement)
.WithMany(a => a.Attachments)
.WillCascadeOnDelete(true);
modelBuilder.Entity<Attachments>()
.HasOptional(a => a.Message)
.WithMany(a => a.Attachments)
.WillCascadeOnDelete(true);
modelBuilder.Entity<Messages>()
.HasRequired(m => m.Advertisement)
.WithMany(u => u.Messages)
.WillCascadeOnDelete(true);