Enable delete

Asked

Viewed 46 times

2

I have my model which has several lists. For example:

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

}

Let’s see that in the model above, I have official reference to that list of notifications and examinations.

I’d like to do the scade only in the notification list. That is, when I go to delete the employee, if he has only connection with notifications, he removes everything, if he has some reference to Exams, will come to Exception.

  • The rest of the system will or will not cascade by default?

  • @Ciganomorrisonmendez No, only this list of this model, other models will not have Cascade enabled

1 answer

2


WillCascadeOnDelete() defined with false must solve:

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);
}    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.