Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

406
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda