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

INotifyCollectionChanged event is lost when notifying collection instance is changed #26023

Closed
AndriySvyryd opened this issue Sep 14, 2021 · 0 comments · Fixed by #28567
Closed
Labels
area-change-tracking closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Sep 14, 2021

Paste to ManyToManyTrackingTestBase:

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public virtual async Task Can_replace_dependent_with_many_to_many(bool async)
{
    var principalKey = 0;
    var newRightKey = 0;

    await ExecuteWithStrategyInTransactionAsync(
        async context =>
        {
            var principal = context.EntityOnes.CreateInstance((e, p) => e.Id = Fixture.UseGeneratedKeys ? 0 : 7711);
            var leftEntity = context.EntityTwos.CreateInstance((e, p) => e.Id = Fixture.UseGeneratedKeys ? 0 : 7721);
            var rightEntity = context.EntityThrees.CreateInstance((e, p) => e.Id = Fixture.UseGeneratedKeys ? 0 : 7731);

            principal.Reference = leftEntity;

            leftEntity.ThreeSkipFull = CreateCollection<EntityThree>();

            leftEntity.ThreeSkipFull.Add(rightEntity);

            if (async)
            {
                await context.AddAsync(principal);
            }
            else
            {
                context.Add(principal);
            }

            ValidateFixup(context, principal, leftEntity, rightEntity);

            if (async)
            {
                await context.SaveChangesAsync();
            }
            else
            {
                context.SaveChanges();
            }

            ValidateFixup(context, principal, leftEntity, rightEntity);

            principalKey = principal.Id;
        },
        async context =>
        {
            var queryable = context.Set<EntityOne>().Where(e => principalKey == e.Id).Include(e => e.Reference.ThreeSkipFull);
            var principal = async ? await queryable.FirstAsync() : queryable.First();

            var leftEntity = context.ChangeTracker.Entries<EntityTwo>().Select(e => e.Entity).Single();
            var rightEntity = context.ChangeTracker.Entries<EntityThree>().Select(e => e.Entity).Single();

            ValidateFixup(context, principal, leftEntity, rightEntity);

            var newLeftEntity = context.EntityTwos.CreateInstance((e, p) => e.Id = Fixture.UseGeneratedKeys ? 0 : 7722);
            var newRightEntity = context.EntityThrees.CreateInstance((e, p) => e.Id = Fixture.UseGeneratedKeys ? 0 : 7732);

            principal.Reference = newLeftEntity;

            newLeftEntity.ThreeSkipFull = CreateCollection<EntityThree>();

            newLeftEntity.ThreeSkipFull.Add(newRightEntity);

            context.Remove(leftEntity);

            context.Remove(rightEntity);

            if (RequiresDetectChanges)
            {
                context.ChangeTracker.DetectChanges();
            }

            if (async)
            {
                await context.SaveChangesAsync();
            }
            else
            {
                context.SaveChanges();
            }

            ValidateFixup(context, principal, newLeftEntity, newRightEntity);
            newRightKey = newRightEntity.Id;
        },
        async context =>
        {
            var queryable = context.Set<EntityOne>().Where(e => principalKey == e.Id).Include(e => e.Reference.ThreeSkipFull);
            var principal = async ? await queryable.FirstAsync() : queryable.First();

            var leftEntity = context.ChangeTracker.Entries<EntityTwo>().Select(e => e.Entity).Single();
            var rightEntity = context.ChangeTracker.Entries<EntityThree>().Select(e => e.Entity).Single();

            ValidateFixup(context, principal, leftEntity, rightEntity);

            Assert.Equal(newRightKey, principal.Reference.ThreeSkipFull.Single().Id);
        });

    static void ValidateFixup(DbContext context, EntityOne principal, EntityTwo leftEntity, EntityThree rightEntity)
    {
        Assert.Equal(4, context.ChangeTracker.Entries().Count());
        Assert.Single(context.ChangeTracker.Entries<EntityOne>());
        Assert.Single(context.ChangeTracker.Entries<EntityTwo>());
        Assert.Single(context.ChangeTracker.Entries<EntityThree>());
        Assert.Single(context.ChangeTracker.Entries<JoinTwoToThree>());

        Assert.Same(leftEntity, principal.Reference);
        Assert.Same(principal, leftEntity.ReferenceInverse);

        Assert.Same(rightEntity, leftEntity.ThreeSkipFull.Single());
        Assert.Same(leftEntity, rightEntity.TwoSkipFull.Single());

        VerifyRelationshipSnapshots(context, new[] { principal });
        VerifyRelationshipSnapshots(context, new[] { leftEntity });
        VerifyRelationshipSnapshots(context, new[] { rightEntity });
    }
}

Currently fails because more entities are detached after SaveChanges when using proxies

@ajcvickers ajcvickers self-assigned this Sep 17, 2021
@ajcvickers ajcvickers added this to the Backlog milestone Sep 17, 2021
@ajcvickers ajcvickers modified the milestones: Backlog, 7.0.0 Nov 10, 2021
@ajcvickers ajcvickers changed the title Potential change-tracking bug for many-to-many INotifyCollectionChanged event is lost when notifying collection instance is changed Aug 1, 2022
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 1, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-rc1 Aug 6, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-rc1, 7.0.0 Nov 5, 2022
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-change-tracking closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants