Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

404
Views
In EF Core Migration when setting Cascade Delete does it not enforce it in the database?

Why when setting Cascade delete between Parent and Child entity does it not create the Cascade in the migration?

Usual Blog / Post example:

class Blog
{
    public int Id { get;set; }
    public IList<Post> Posts { get;set;}
}

class Post
{
    public int Id { get;set; }
    public Blog Blog { get;set;}
}

In the EntityTypeConfiguration file

public override void Configure(EntityTypeBuilder<Notification> builder)
    {
 
        builder.HasMany(n => n.Posts).WithOne(e => e.Blog)
            .OnDelete(DeleteBehavior.Cascade);
    }

Why does it create the migration script of?

            ...
            migrationBuilder.AddForeignKey(
            name: "FK_Posts_Blogs_BlogId",
            table: "Posts",
            column: "BlogId",
            principalTable: "Blogs",
            principalColumn: "Id",
            onDelete: ReferentialAction.Restrict);
            ...

Note the

onDelete: ReferentialAction.Restrict

I understand that EF will actually do the cascade delete internally as long as you include the child objects for it to work through but why not leverage the Database Services cascade delete to be able to delete it in one command rather than 1 + n SQL commands i.e. 1 x Blog record & n x posts.

Imagine that there are 1000s of posts and you are deleting a blog.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

After looking through the project which I was trying to do this in a realised that the Dbcontext was inherited from a base class which had this code in...

private void ConfigureEntities(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfigurationsFromAssembly(GetType().Assembly);

        var entityTypes = modelBuilder.Model
            .GetEntityTypes()
            .ToList();

        // Disable cascade delete
        var foreignKeys = entityTypes
            .SelectMany(e => e.GetForeignKeys().Where(f => f.DeleteBehavior == DeleteBehavior.Cascade));
        foreach (var foreignKey in foreignKeys)
        {
            foreignKey.DeleteBehavior = DeleteBehavior.Restrict;
        }
    }

In the end I copied this into the Gist Project and replicated it straight away.

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.ApplyConfigurationsFromAssembly(typeof(TestContext).Assembly);

        var entityTypes = modelBuilder.Model
            .GetEntityTypes()
            .ToList();
        var foreignKeys = entityTypes
        .SelectMany(e => e.GetForeignKeys().Where(f => f.DeleteBehavior == DeleteBehavior.Cascade));
        foreach (var foreignKey in foreignKeys)
        {
            foreignKey.DeleteBehavior = DeleteBehavior.Restrict;
        }

    }

I'm not sure why this was done but plan to find out, I think it came from a previous Template that we were using.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!