Galma88
Galma88

Reputation: 2546

Entity Framework 6.12 cascade Delete ( one to many relationship)

i have two table parent(id p_key,name) and child(addresid,city, id ForeignKey) table have one to many relationship ,

so if i am deleting any recording from parent table then all related record should be deleted from child table

i am using entity framework code first approach

Upvotes: 1

Views: 1566

Answers (1)

Salah Akbari
Salah Akbari

Reputation: 39966

Add this to your DB Context:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<parent>()
    .HasOptional(c => c.child)
    .WithOptionalDependent()
    .WillCascadeOnDelete(true);
}

Have a look at this:Enabling Cascade Delete

Upvotes: 2

Related Questions