Set CollectionEntry.IsModified to `true` when removing items from a collection
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
Research direction
Start with src/EFCore/ChangeTracking/CollectionEntry.cs, especially the IsModified logic around lines 138-144, and the reproduction fixture in test/EFCore.SqlServer.FunctionalTests/SqlServerEndToEndTest.cs. Run or extend the shown removal test; done means DetectChanges makes the collection entry report IsModified as true after an item is removed.
Written by the indexing model from the issue text.
Description
File a bug
The IsModified property on a CollectionEntry does not correctly reflect it has been modified if the action is only to remove an entry from the collection.
The intent seems to be for that field to reflect that, given in the source code there are code checks for entries with a State of Deleted and previous work done for https://github.com/dotnet/efcore/issues/10450.
Include your code
This is easy to reproduce adding some additional cases to your SqlServerEndToEndTests fixture, which I've tested in my own efcore fork:
[ConditionalFact]
public void Removing_an_item_from_a_collection_marks_it_as_modified()
{
using var testDatabase = SqlServerTestStore.CreateInitialized(DatabaseName);
var options = Fixture.CreateOptions(testDatabase);
using var context = new GameDbContext(options);
context.Database.EnsureCreatedResiliently();
var player = new PlayerCharacter(
new Level { Game = new Game() });
var weapon = new Item { Id = 1, Game = player.Game };
player.Items.Add(weapon);
context.Characters.Add(player);
context.SaveChanges();
player.Items.Remove(weapon);
context.ChangeTracker.DetectChanges();
// this fails
Assert.True(context.Entry(player).Collection(p => p.Items).IsModified);
}
⚠️ As I was testing these behaviours I noticed that attempting to delete the weapon entity after setting it as player.CurrentWeapon = weapon; would throw an exception as it seems EF tries to clear both foreign keys for CurrentWeapon in PlayerCharacter - CurrentWeaponId and GameId - however because GameId is used in other required relationships that causes an exception to be thrown. Clearing the weapon player.CurrentWeapon = null; as opposed to deleting the entity context.Remove(weapon); seems to work fine, so perhaps that's a possible bug as well?
Include stack traces
n/a
Include verbose output
n/a
Include provider and version information
Using latest released EF Core version (3.1.1) targeting .NET 5.0
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 134
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
customer-reported
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
area-cosmos area-vector-search
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-cosmos
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-tools needs-design
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100