Share via

Using TPC it now does not cascade delete child objects

David Thielen 3,231 Reputation points
2023-07-17T16:53:42.2033333+00:00

Hi all;

So I have a base class of Effort:

public abstract class Effort 
{
    public int Id { get; private set; }
    public ICollection<Signup>? Signups { get; set; }
    // ...
}

public class EffortConfiguration : IEntityTypeConfiguration<Effort>
{
    public void Configure(EntityTypeBuilder<Effort> builder)
    {
        builder.UseTpcMappingStrategy();
    }
}

And it has 2 sub classes:

public class Job : Effort
{
    // everything is in the base class
}

public class Event : Effort
{
    public ICollection<Shift>? Shifts { get; set; }
    // ...
}

And then we have the children (Signup & Shift):

public class Signup 
{
    public int Id { get; private set; }
    public Effort Effort { get; set; }
    // ...
}

public class Shift : IPkIsId
{
    public int Id { get; private set;  }
    public Event Event { get; set; } = default!;
    // ...
}

So when an Event is deleted, all of the Shift children are deleted. But when an Event or Job is deleted, the children Signup records are not deleted.

They are deleted if I don't call builder.UseTpcMappingStrategy().

I have tried fluent. In the SignupConfiguration I tried:

builder.HasOne(s => s.Effort)
    .WithMany(e => e.Signups)
    .OnDelete(DeleteBehavior.Cascade);

In the Event & JobConfiguration I tried (also tried it in Effort):

builder.HasMany(s => s.Signups)
    .WithOne(e => (Job?)e.Effort)
    .OnDelete(DeleteBehavior.Cascade);

Aside from this one problem TPC is working great. All my other unit tests pass. I get the two distinct tables.

I also tried moving public ICollection<Signup>? Signups { get; set; } to Event & Job, removing it from Effort. But that got an error during update-database where it said it was trying to create a duplicate function name.

What do I need to do here?

thanks - dave

Developer technologies | .NET | Entity Framework Core

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.