Enable delete cascade in only one list

2

I have my model that has multiple lists. For example:

public class Funcionario
{
   public ICollection<FuncionarioNotificacao> Notificacoes {get;set;}
   public ICollection<FuncionarioExame> Exames {get;set;}

}

Let's see that in model above, I have employee reference to this list of notifications and exams.

I'd like to make cascade just in the notification list. That is, when I am going to delete the employee, if he has only link with notifications, he removes everything, if he has any referrals from Exams, he will come to exception .     

asked by anonymous 27.03.2015 / 15:12

1 answer

2

WillCascadeOnDelete() set with false should resolve:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{

    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Funcionario>().
      HasMany(f => f.Notificacoes).
      WithRequired(n => n.Funcionario).
      HasForeignKey(n => n.FuncionarioId).
      WillCascadeOnDelete(false);
}    
    
27.03.2015 / 19:11