What is different between DeleteBehavior.NoAction and DeleteBehavior.Restrict in EntityFramework Core?

Viewed 2995

I have a configuration like this in my codes:

builder.HasMany(c => c.Libs)
       .WithOne(x=>x.Book)
       .HasForeignKey(x=>x.BookId)
       .OnDelete(DeleteBehavior.NoAction);  <<-------- Here

My question is what is different between NoAction and Restrict value?

I read Microsoft document and descriptions of both of them are same!!!

enter image description here

2 Answers

The delete behavior defines the action that will be taken when the parent entity in a PK-FK relationship is deleted. In this case, the parent entity is Book and the Child entity is Libs, with 1:many relationship.

NoAction: In this case, when a row from Book table is deleted from the database, all the rows from Libs table that have BookId same as deleted BookId, will be converted to null. Note that this method does not delete any items from Libs table, and can leave them orphaned.

Restrict: In this case, when a row from Book table can only be deleted from the database if there are no associated Libs with the same BookId. Note that you can only delete the Book row once you have deleted all the associated Libs.

Cascade: In this case, all the associated Libs rows from the books will be automatically deleted if the Book row is deleted.

Related