Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascade Delete in client (application server) only, without having cascade delete in Db. #12168

Closed
kmolerov opened this issue May 30, 2018 · 3 comments

Comments

@kmolerov
Copy link

kmolerov commented May 30, 2018

Feature request - ability to configure cascade delete on client side only (application server), without having cascade delete in Db.
When having aggregate root with all its includes in memory and remove just the root entity from context - all included entities should be marked for delete (EntityState.Deleted ).

Possible solution - add additional DeleteBehaviors:
DeleteBehavior.CascadeInClient which will not expect (and generate trough migrations) ON DELETE CASCADE to be present in the Db.

Steps to reproduce - Example:

//model
class Parent{
  public int Id { get; set; } 
  public Child Child { get; set; }
}
class Child{
 public int Id { get; set; } // Pk & Fk to Parent
}

// configuration
modelBuilder.Entity<Parent>()
 .HasOne(e => Child)
 .WithOne()
 .HasForeignKey<Child>()
.OnDelete(DeleteBehavior.CascadeInClient);

// example code
var model = context.Set<Parent>()
  .Single(e=>e.Id == 1)
  .Include(e=>e.Child);

context.Remove(model); 
context.SaveChanges();
 // This should set EntityState.Deleted to both model & model.Child and compose query to delete them from Db, without having cascade delete inside Db.

// This should be applicable to One-to-Many also.
// Migrations should create db tables with ON DELETE RESTRICT

####Workarounds
Use reflection to recursively iterate over loaded navigation properties and set EntityState.Deleted on all child entities.

Further technical details

EF Core version: 2.0
Database Provider: Sql server & PgSql
Operating system: Windows 8.1
IDE: (e.g. Visual Studio 2017 15.4)

@ajcvickers
Copy link
Contributor

Note for triage--related to aggregates behavior #1985

@kmolerov If you edit the generated Migrations to not generate cascades in the database, then the result should be what you are looking for.

@ajcvickers
Copy link
Contributor

Putting this on the backlog, but whether it gets done or not will likely depend on the outcome of the aggregates design.

@roji
Copy link
Member

roji commented Jul 5, 2020

Duplicate of #18960? We already have ClientCascade, but that currently cascades only for tracked entities.

Another related/possibly duplicated issue: #10179

@roji roji removed this from the Backlog milestone Jul 5, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants