Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which part of this relationship WillCascadeOnDelete(true)?

This is what I have today:

modelBuilder.Entity<User>()
    .HasOptional(p => p.DealDevice)
    .WithRequired(c => c.User)
    .WillCascadeOnDelete(false);

What I would like is to have the related DealDevice rows be deleted if the user is deleted so that I can just delete the user and have everything related go away.

Can I just change WillCascadeOnDelete(false) to WillCascadeOnDelete(true) or will that delete the user if the deal is deleted?

like image 814
Jed Grant Avatar asked Jun 05 '14 19:06

Jed Grant


1 Answers

You are correct: to cascade deletes from User to DealDevice just use WillCascadeOnDelete(true). It's not going to delete the User if a DealDevice is deleted, only the other way around.

modelBuilder.Entity<User>()
    .HasOptional(p => p.DealDevice)
    .WithRequired(c => c.User)
    .WillCascadeOnDelete();

Note: you don't have to specify the parameter to WillCascadeOnDelete() if you want to cascade deletes as there's an overload implicitly setting it to true.

like image 126
andyp Avatar answered Sep 28 '22 13:09

andyp