-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Use local DetectChanges when cascade-deleting #22079
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1696,5 +1696,124 @@ public void Can_set_set_to_Unchanged_with_inconsistent_FK_dependent_first_refere | |||||
Assert.Equal(EntityState.Unchanged, context.Entry(category).State); | ||||||
Assert.Equal(EntityState.Unchanged, context.Entry(product).State); | ||||||
} | ||||||
|
||||||
[ConditionalTheory] // Issue #17828 | ||||||
[InlineData(CascadeTiming.Immediate)] | ||||||
[InlineData(CascadeTiming.Never)] | ||||||
[InlineData(CascadeTiming.OnSaveChanges)] | ||||||
public void Can_re_parent_optional_without_DetectChanges(CascadeTiming cascadeTiming) | ||||||
{ | ||||||
using var context = new Parent77Context(); | ||||||
|
||||||
context.ChangeTracker.CascadeDeleteTiming = cascadeTiming; | ||||||
|
||||||
var parent1 = new Parent77(); | ||||||
var parent2 = new Parent77(); | ||||||
var child = new Optional77(); | ||||||
|
||||||
child.Parent77 = parent1; | ||||||
context.AddRange(parent1, parent2, child); | ||||||
context.SaveChanges(); | ||||||
|
||||||
Assert.Equal(3, context.ChangeTracker.Entries().Count()); | ||||||
Assert.Equal(EntityState.Unchanged, context.Entry(parent1).State); | ||||||
Assert.Equal(EntityState.Unchanged, context.Entry(parent2).State); | ||||||
Assert.Equal(EntityState.Unchanged, context.Entry(child).State); | ||||||
|
||||||
child.Parent77 = parent2; | ||||||
context.Remove(parent1); | ||||||
|
||||||
Assert.Equal(3, context.ChangeTracker.Entries().Count()); | ||||||
Assert.Equal(EntityState.Deleted, context.Entry(parent1).State); | ||||||
Assert.Equal(EntityState.Unchanged, context.Entry(parent2).State); | ||||||
Assert.Equal(EntityState.Modified, context.Entry(child).State); | ||||||
Assert.Same(parent2, child.Parent77); | ||||||
|
||||||
context.SaveChanges(); | ||||||
|
||||||
Assert.Equal(2, context.ChangeTracker.Entries().Count()); | ||||||
Assert.Equal(EntityState.Detached, context.Entry(parent1).State); | ||||||
Assert.Equal(EntityState.Unchanged, context.Entry(parent2).State); | ||||||
Assert.Equal(EntityState.Unchanged, context.Entry(child).State); | ||||||
Assert.Same(parent2, child.Parent77); | ||||||
} | ||||||
|
||||||
[ConditionalTheory] // Issue #17828 | ||||||
[InlineData(CascadeTiming.Immediate)] | ||||||
[InlineData(CascadeTiming.Never)] | ||||||
[InlineData(CascadeTiming.OnSaveChanges)] | ||||||
public void Can_re_parent_required_without_DetectChanges(CascadeTiming cascadeTiming) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
using var context = new Parent77Context(); | ||||||
|
||||||
context.ChangeTracker.CascadeDeleteTiming = cascadeTiming; | ||||||
|
||||||
var parent1 = new Parent77(); | ||||||
var parent2 = new Parent77(); | ||||||
var child = new Required77(); | ||||||
|
||||||
child.Parent77 = parent1; | ||||||
context.AddRange(parent1, parent2, child); | ||||||
context.SaveChanges(); | ||||||
|
||||||
Assert.Equal(3, context.ChangeTracker.Entries().Count()); | ||||||
Assert.Equal(EntityState.Unchanged, context.Entry(parent1).State); | ||||||
Assert.Equal(EntityState.Unchanged, context.Entry(parent2).State); | ||||||
Assert.Equal(EntityState.Unchanged, context.Entry(child).State); | ||||||
|
||||||
child.Parent77 = parent2; | ||||||
context.Remove(parent1); | ||||||
|
||||||
Assert.Equal(3, context.ChangeTracker.Entries().Count()); | ||||||
Assert.Equal(EntityState.Deleted, context.Entry(parent1).State); | ||||||
Assert.Equal(EntityState.Unchanged, context.Entry(parent2).State); | ||||||
Assert.Equal(EntityState.Modified, context.Entry(child).State); | ||||||
Assert.Same(parent2, child.Parent77); | ||||||
|
||||||
context.SaveChanges(); | ||||||
|
||||||
Assert.Equal(2, context.ChangeTracker.Entries().Count()); | ||||||
Assert.Equal(EntityState.Detached, context.Entry(parent1).State); | ||||||
Assert.Equal(EntityState.Unchanged, context.Entry(parent2).State); | ||||||
Assert.Equal(EntityState.Unchanged, context.Entry(child).State); | ||||||
Assert.Same(parent2, child.Parent77); | ||||||
} | ||||||
|
||||||
private class Parent77Context : DbContext | ||||||
{ | ||||||
protected internal override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||||||
=> optionsBuilder.UseInMemoryDatabase(nameof(Parent77Context)); | ||||||
|
||||||
protected internal override void OnModelCreating(ModelBuilder modelBuilder) | ||||||
{ | ||||||
modelBuilder.Entity<Parent77>( | ||||||
b => | ||||||
{ | ||||||
b.HasMany<Optional77>().WithOne(e => e.Parent77); | ||||||
b.HasMany<Required77>().WithOne(e => e.Parent77); | ||||||
}); | ||||||
} | ||||||
} | ||||||
|
||||||
private class Parent77 | ||||||
{ | ||||||
public int Id { get; set; } | ||||||
} | ||||||
|
||||||
private class Optional77 | ||||||
{ | ||||||
public int Id { get; set; } | ||||||
|
||||||
public int? Parent77Id { get; set; } | ||||||
public Parent77 Parent77 { get; set; } | ||||||
} | ||||||
|
||||||
private class Required77 | ||||||
{ | ||||||
public int Id { get; set; } | ||||||
|
||||||
public int Parent77Id { get; set; } | ||||||
public Parent77 Parent77 { get; set; } | ||||||
} | ||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.