dotnet / dotnet/efcore

Cosmos: removing a value from an owned collection leaves the change tracker in a bad state

Open
#33,687 0 comments 0 reactions 1 assignee Assigned to @AndriySvyryd View on GitHub
area-cosmos area-save-changes
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

`EmbeddedDocumentsTest`:
```C#
[Fact]
public virtual async Task Can_manipulate_embedded_collection_and_clear_it()
{
var options = await Fixture.CreateOptions();

await using var context = new EmbeddedTransportationContext(options);

var existingAddress1Person3 = new Address
{
Street = "First",
City = "City",
AddressTitle = new AddressTitle { Title = "P3 Shipping" }
};
var existingAddress2Person3 = new Address
{
Street = "Second",
City = "City",
AddressTitle = new AddressTitle { Title = "P3 Billing" }
};
var person = new Person { Id = 3, Addresses = new List

{ existingAddress1Person3, existingAddress2Person3 } };
context.Add(person);

await context.SaveChangesAsync();

var addresses = ((IList

)person.Addresses);
addresses.RemoveAt(0);
await context.SaveChangesAsync();

addresses.Clear();
await context.SaveChangesAsync();
}
```

> System.InvalidOperationException : The property 'Address.Id' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key, first delete the dependent and invoke 'SaveChanges', and then associate the dependent with the new principal.
ChangeDetector.ThrowIfKeyChanged(InternalEntityEntry entry, IProperty property) line 74
ChangeDetector.DetectValueChange(InternalEntityEntry entry, IProperty property) line 286
ChangeDetector.LocalDetectChanges(InternalEntityEntry entry) line 235
ChangeDetector.DetectChanges(InternalEntityEntry entry, HashSet`1 visited) line 208
ChangeDetector.DetectChanges(InternalEntityEntry entry, HashSet`1 visited) line 201
ChangeDetector.DetectChanges(InternalEntityEntry entry) line 178
StateManager.CascadeDelete(InternalEntityEntry entry, Boolean force, IEnumerable`1 foreignKeys) line 1202
StateManager.CascadeChanges(Boolean force) line 1166
StateManager.GetEntriesToSave(Boolean cascadeChanges) line 1124
StateManager.SaveChangesAsync(StateManager stateManager, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken) line 1401
<b__0>d.MoveNext() line 308
--- End of stack trace from previous location ---
ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken) line 331
ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken) line 355
ExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken) line 307
DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken) line 791
DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken) line 826
EmbeddedDocumentsTest.Can_manipulate_embedded_collection_and_clear_it() line 131
EmbeddedDocumentsTest.Can_manipulate_embedded_collection_and_clear_it() line 131
--- End of stack trace from previous location ---

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.