C#C
C#2y ago
oe

Entity Framework Cascade Delete Issues

Hi guys, I want to be able to save this as a single operation:
DBContext:
public DbSet<ProductNode> ProductNodes { get; set; } = null!;
public DbSet<VirtualStore> VirtualStore { get; set; } = null!;
modelBuilder.Entity<ProductNode>()
    .HasOne(lpn => lpn.VirtualProduct)
    .WithOne()
    .OnDelete(DeleteBehavior.Cascade);

....
BackgroundService:
vphContext.VirtualStore.RemoveRange(virtualStoresInUse);
vphContext.ProductNodes.RemoveRange(nodesToRemove);
await vphContext.SaveChangesAsync(stoppingToken);

vphContext.VirtualStore.AddRange(newStores);
vphContext.ProductNodes.AddRange(newProductNodes);
await vphContext.SaveChangesAsync(stoppingToken);


However, for some reason, ProductNodes does not get re-added if I remove the first "SaveChangesAsync".

The reason I want it in a single operation, is I don't want downtime where there is no VirtualStore list or ProductNode list between the both operations.
Was this page helpful?