I have a Group model/table:
namespace Project
{
#region Usings
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
#endregion
[Table("Groups")]
public class Group
{
public Group()
{
this.Pk = Guid.NewGuid();
}
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Key]
[Required]
public Guid Pk
{
get;
set;
}
public virtual ICollection<Group> Parents
{
get;
set;
}
public virtual ICollection<Group> Children
{
get;
set;
}
public virtual ICollection<Document> Documents
{
get;
set;
}
...
}
A group can have many groups as either a child or parent, as well as many documents.
I have the relationships wired up in Fluent API as so:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Group>()
.HasMany(e => e.Parents)
.WithMany(e => e.Children)
.Map(e =>
{
e.MapLeftKey("ParentPk");
e.MapRightKey("ChildPk");
e.ToTable("GroupMappings");
});
modelBuilder.Entity<Group>()
.HasMany(e => e.Documents)
.WithMany(e => e.Groups)
.Map(e =>
{
e.MapLeftKey("DocumentPk");
e.MapRightKey("GroupPk");
e.ToTable("DocumentMappings");
});
}
When I generate the SQL for these models, the relationship between Groups and Documents (DocumentMappings) has ON DELETE CASCASDE:
ALTER TABLE [dbo].[DocumentMappings] ADD CONSTRAINT [FK_dbo.DocumentMappings_dbo.Documents_GroupPk] FOREIGN KEY ([GroupPk]) REFERENCES [dbo].[Documents] ([Pk]) ON DELETE CASCADE
But the GroupMappings does not:
ALTER TABLE [dbo].[GroupMappings] ADD CONSTRAINT [FK_dbo.GroupMappings_dbo.Groups_ParentPk] FOREIGN KEY ([ParentPk]) REFERENCES [dbo].[Groups] ([Pk])
ALTER TABLE [dbo].[GroupMappings] ADD CONSTRAINT [FK_dbo.GroupMappings_dbo.Groups_ChildPk] FOREIGN KEY ([ChildPk]) REFERENCES [dbo].[Groups] ([Pk])
Visual representation of the tables:
What could be wrong with my mappings for Group (Parent/Children) that is causing ON DELETE CASCADE to not be "enabled"? Is it related to it being a reference on the same type?
Disclaimer: I have seen a few posts saying this can not be done because you "can't use CASCADE DELETE on self referencing table in SQL SERVER." (Entity Framework 6 Code-First cascade delete on self referencing entity). I am using a mapping table in this case, so this should not be the situation.