C
C#5mo 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);
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.
0 Replies
No replies yetBe the first to reply to this messageJoin