© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y 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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Entity Framework Migration Issues
C#CC# / help
3y ago
[Entity Framework] Issues with .Include()
C#CC# / help
4y ago
Entity framework
C#CC# / help
14mo ago
❔ Entity Framework
C#CC# / help
3y ago