Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

410
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda