I have 2 classes:
public class Transaction : AggregateRoot<Guid>
{
private readonly IList<Correction> _corrections = new List<Correction>();
public IEnumerable<Correction> Corrections => _corrections;
public void ClearCorrections()
{
_corrections.Clear();
}
}
public class Correction : Transaction
{
....
}
My context is as followed:
modelBuilder.Entity<Transaction>().HasMany(p => p.Corrections).WithOne().HasForeignKey("ParentTransactionId").OnDelete(DeleteBehavior.NoAction).IsRequired();
When I clear my collection of corrections and db.SaveChanges, database is updated putting ParentTransactionId to null. Instead I would like to delete them from database.
I tried composite key on Correction but EFcore doesn't want as i cannot specify composite Key on a child.
For now, what I do is :
public async Task DeleteCorrectionsAsync(Transaction transaction, CancellationToken cancellationToken)
{
....
foreach (var correction in transaction.Corrections) await Repository.RemoveAsync(correction, cancellationToken);
transaction.ClearCorrections();
}
Is there any way i can remove the await Repository.RemoveAsync(correction, cancellationToken); ? I am sure it has been asked 1000 times, but can't find the answer... Thanks !