简体   繁体   中英

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 ie 1 x Blog record & nx posts.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM