Search code examples
c#entity-framework-corecascadeaspnetboilerplatesoft-delete

Soft delete with cascade deletion doesn't work as expected


I'm using EF Core and SQLite in unit tests. Given the following 1-to-1 entities:

public class Entity1 : FullAuditedEntity<int>
{
    public Entity2 Entity2 { get; set; }

    public string Name { get; set; }
}


public class Entity2 : FullAuditedEntity<int>
{
    public string Name { get; set; }       

    [ForeignKey("Entity1Id")]
    public Entity1 Entity1 { get; set; }
    public int Entity1Id { get; set; }
}

DbContext class has the following code:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    ...

    modelBuilder.Entity<Entity1>().HasOne(t => t.Entity2)
                                  .WithOne(t1 => t1.Entity1)
                                  .OnDelete(DeleteBehavior.Cascade);

    ...
}

I have a test method:

[Fact]
public async Task Should_Create_And_Then_Delete_Single_Entity1()
{
    var entity1Service = Resolve<Entity1Service>();
    var entity1Repo = Resolve<IRepository<Entity1>>();
    var entity2Repo = Resolve<IRepository<Entity2>>();
    var entity1 = new entity1 { Name = "ent1" };

    entity2Repo.Count().ShouldBe(0);

    // entity2 created also, see assert below
    var created = await entity1Service.CreateEntity1Async(entity1).ConfigureAwait(false);
    created.Id.ShouldBe(1);
    created.Name.ShouldBe("net1");

    entity1Repo.Count().ShouldBe(1);
    entity2Repo.Count().ShouldBe(1);

    var ent = await entity1Service.GetEntity1Async(created.Id).ConfigureAwait(false);
    ent.ShouldNotBeNull();
    ent.Entity2.ShouldNotBeNull();

    await entity1Service.DeleteEntity1Async(ent.Id).ConfigureAwait(false);

    entity1Repo.Count().ShouldBe(0);
    entity2Repo.Count().ShouldBe(0);
}

The problem is that the last line of code, "entity2Repo.Count().ShouldBe(0);" assertion is broken, it is actually 1 instead of 0, IsDeleted (soft delete) is false, but I expect it to be true.

What am I doing wrong?

Thanks in advance.


Solution

  • You need to cascade soft deletes yourself.
    See the rationale in the closed PR aspnetboilerplate/aspnetboilerplate#3559.

    You can do that by defining an event handler:

    public class Entity1DeletingCascader : IEventHandler<EntityDeletingEventData<Entity1>>, ITransientDependency
    {
        private readonly IRepository<Entity2> _entity2Repository;
    
        public Entity1DeletingCascader(IRepository<Entity2> entity2Repository)
        {
            _entity2Repository = entity2Repository;
        }
    
        [UnitOfWork]
        public virtual void HandleEvent(EntityDeletingEventData<Entity1> eventData)
        {
            var entity1 = eventData.Entity;
            _entity2Repository.Delete(e2 => e2.Entity1Id == entity1.Id);
        }
    }