I was wondering if there was a good way to delete related records, just like you can get all the records with WithDetailsAsync()?
Or will I just have to add all the services (of the related entities I want to delte) to the main one (channelAppService in my example) and iterate through them and manually delete each one (see example 1)?
/* code in my ChannelAppService */
public async Task<ChannelDto> GetWithDetailsAsync(Guid id)
{
//Get the related records General and Settings from _channelRepository
var queryable = await _channelRepository.WithDetailsAsync(x => x.General, x => x.Settings);
//Where Channel is == id
var query = queryable.Where(x => x.Id == id);
//Get Channel with General and Settings back
return ObjectMapper.Map<Channel, ChannelDto>(await AsyncExecuter.FirstOrDefaultAsync(query));
}
// What would be the recomended way to delete the related records of channel?
public async Task DeleteWithDetailsAsync(Guid id)
{
// 1. example
// But what if the first delete works but the rest fail?
_channelRepository.DeleteAsync(id);
_generalRepository.DeleteAsync(theGeneralId);
_settingsRepository.DeleteAsync(theSettingsId);
// 2. example
//OR is there simpler way like this maybe (with some more code obviously) ?
await AsyncExecuter.DeleteWithDetails(channel);
}
This must be fairly common to do so isn't there some examples you can share with me?
- ABP Framework version: v4.2.2
- UI type: Blazor
- DB provider: EF Core
- Identity Server Separated: yes
3 Answer(s)
-
0
Ok I think I am somewhere near the answer with .OnDelete(DeleteBehavior.Cascade) but for some reason calling await ChannelAppService.DeleteAsync(id); just marks the Channel entity as deleted (IsDeleted == true) but General doesn't get deleted..
builder.Entity<Channel>(b => { b.ToTable("Channel"); b.ConfigureByConvention(); //Define the relation b.HasOne(x => x.General) .WithOne(x => x.Channel) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); }); builder.Entity<General>(b => { b.ToTable("Generals"); b.ConfigureByConvention(); });
I'm just calling this from a Blazor page
await ChannelAppService.DeleteAsync(id);
I know this is likely just EF stuff but since I have wasted 1 question I might as well get the help after all :-)
-
0
Hi,
Yes, Because some entitie are soft deleted, related entities cannot be soft deleted automatically. You can use domain events, when the entity is deleted, manually delete the related implementation.
https://docs.abp.io/en/abp/latest/Local-Event-Bus#events-with-past-tense
-
0
Thak you! Worked like a charm