I have two entities. Profile
and ProfileImages
. After fetching a Profile
I want to delete ProfileImages
through Pro
You can add this to your DB Context
:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Profile>()
.HasOptional(c => c.ProfileImages)
.WithOptionalDependent()
.WillCascadeOnDelete(true);
}
Read more here:Enabling Cascade Delete
You can configure cascade delete on a relationship by using the WillCascadeOnDelete method. If a foreign key on the dependent entity is not nullable, then Code First sets cascade delete on the relationship. If a foreign key on the dependent entity is nullable, Code First does not set cascade delete on the relationship, and when the principal is deleted the foreign key will be set to null.