Skip to content

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

@AndriySvyryd

Description

@AndriySvyryd

EmbeddedDocumentsTest:

    [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<Address> { existingAddress1Person3, existingAddress2Person3 } };
        context.Add(person);

        await context.SaveChangesAsync();

        var addresses = ((IList<Address>)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, HashSet1 visited) line 208 ChangeDetector.DetectChanges(InternalEntityEntry entry, HashSet1 visited) line 201
ChangeDetector.DetectChanges(InternalEntityEntry entry) line 178
StateManager.CascadeDelete(InternalEntityEntry entry, Boolean force, IEnumerable1 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 <<ExecuteAsync>b__0>d.MoveNext() line 308 --- End of stack trace from previous location --- ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func4 operation, Func4 verifySucceeded, TState state, CancellationToken cancellationToken) line 331 ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func4 operation, Func4 verifySucceeded, TState state, CancellationToken cancellationToken) line 355 ExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 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 ---

Metadata

Metadata

Assignees

Type

No fields configured for Bug.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions