Cascading Delete in Entity Framework with Fluent API

What is Cascading Delete ?

There may be situation where if a record in parent table is deleted than corresponding records in child tables(Foreign Key ) are also deleted.

Parent and Child Mapping with Fluent Api in Entity Framework:

I am not going to describe how to map parent and child table with fluent api. you can read it more about here.

In the below table diagram, the AssessmentChatRoom is the base parent whose foreign key is referenced by ChatRoomData and ChatRoomParticipants table.

Capture

Mapping the relation with FluentAPI and Setting the Cascade Delete:

AssessmentChatRoom is the parent and ChatRoomData and ChatRoomParticipants are the child tables.In the ChatRoomData WillCascadeDelete() Property is set to true.

 

Parent Table:


public class AssesmentChatRoomConfig
 {
  public AssesmentChatRoomConfig()
{
ToTable("AssessmentChatRooms").HasKey(e => e.Id);
Property(e => e.Id).HasColumnName("Id");
Property(e => e.Name).HasColumnName("Name");
Property(e => e.LastUpdatedOn).HasColumnName("LastUpdatedOn");
Property(e => e.CreatedOn).HasColumnName("CreatedOn");
Property(e => e.AssessmentChatRoomParentId).HasColumnName("AssessmentChatRoomParentId");
}
 }

ChildTable


public class ChatRoomDataConfig 
 {
public ChatRoomDataConfig()
{
ToTable("ChatRoomData").HasKey(e => e.Id);
Property(e => e.Id).HasColumnName("Id");
Property(e => e.AssessmentChatRoomId).HasColumnName("AssessmentChatRoomId");
Property(e => e.ChatText).HasColumnName("ChatText");
Property(e => e.PostedBy).HasColumnName("PostedBy");
Property(e => e.TimeStamp).HasColumnName("TimeStamp");
Property(e => e.MessageType).HasColumnName("MessageType");
HasRequired(s => s.AssessmentChatRoom)
.WithMany(s => s.ChatRoomDatas).HasForeignKey(s => s.AssessmentChatRoomId).
WillCascadeOnDelete(true); // set Cascade delete
}
}

Deleting the Parent Table with Cascade Delete :

1.Get the parent record id and its related child record.


var result =dbcontext.Assessmentchatroom.where(x=>x.id == id);

 

 

The above query will bring only the parent record and you might see the child records to be null,this is because the entity framework by default has lazy loading enabled. It is not a good idea to disable lazy loading,instead use include keyword and retrieve the child elements.

Retrieving Child Record using Include Keyword of entity framework:

Its possible to let know the parent to load the child elements using include keyword as below.

var entity = Context.AssessmentChatRooms.
Include(c=>c.ChatRoomDatas.Select(b=>b.AssessmentChatRoom)).
where(x=>x.id =id)

Code to Remove :

This is a pretty standard way to remove.you just got to pass the parent entity and got to call the Remove method of entity framework and this will in turn delete all the child elements,since cascade delete is set to true.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s