Home > Back-end >  How can I set onDelete cascade in this situation in EF Core?
How can I set onDelete cascade in this situation in EF Core?

Time:03-29

ApplicationDbContext:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<Post>().Property(p => p.Name).IsRequired();
    builder.Entity<PostTag>().HasKey(pt => new { pt.PostId, pt.TagId});
    builder.Entity<PostTag>().HasKey(pt => new { pt.PostId, pt.TagId});
            
    builder.Entity<PostCategory>().HasKey(pc => new { pc.PostId, pc.CategoryId });
}

How to achieve OnDelete Cascade behaviour?

CodePudding user response:

In Entity Framework Core, the OnDelete Fluent API method is used to specify the delete behavior for a dependent entity when the principal is deleted. Cascade delete is set by default in EF Core, but you can check all details on the fowling link for all cases you can use it OnDelete

https://entityframeworkcore.com/saving-data-cascade-delete

  • Related