Entity Framework auto relationship enable cascading delete

4

How to create a cascading deletion in a self-relationship using fluent api and enable it in entity framework ?

Code:

 Public class Usuario {

    public int UsuarioID { get; set; }
    public int? ObjPaiID { get; set; }
    public virtual Usuario ObjPai { get; set; }
    public virtual IList<Usuario> ListaPartes { get; set; }
}

Context:

//Auto-relacionamento Projeto
   modelBuilder.Entity<Usuario>()
   .HasOptional(p => p.ObjPai)
   .WithMany(p => p.ListaPartes)
   .HasForeignKey(p => p.ObjPaiID);
    
asked by anonymous 22.01.2015 / 16:22

1 answer

4

You can use .WillCascadeOnDelete();

Example:

//Auto-relacionamento Projeto
   modelBuilder.Entity<Usuario>()
   .HasOptional(p => p.ObjPai)
   .WithMany(p => p.ListaPartes)
   .HasForeignKey(p => p.ObjPaiID)
   .WillCascadeOnDelete(true);
    
22.01.2015 / 17:27