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

No state change for removed many-to-many (skip navigation) relation after calling DetectChanges. #4799

Open
walter-rnd opened this issue Sep 5, 2024 · 0 comments
Assignees

Comments

@walter-rnd
Copy link

The state of a removed many-to-many relation is deleted and stays deleted even after it is add again.
Or a other way to describe it is calling Detect Changes breaks the change tracker.
It looks like the change tracker DetectChanges only works the first time.

Is this by design? if so is there a way to change this behavior? (maybe with a payload)

I change the Many_to_many_relationships_3 sample function for clarification:


    public static void Many_to_many_relationships_3()
    {
        Console.WriteLine($">>>> Sample: {nameof(Many_to_many_relationships_3)}");
        Console.WriteLine();

        Helpers.RecreateCleanDatabase();
        Helpers.PopulateDatabase();

        #region Many_to_many_relationships_3
        using var context = new BlogsContext();

        var post = context.Posts.Single(e => e.Id == 3);
        var tag = context.Tags.Single(e => e.Id == 1);

        post.Tags.Add(tag);

        context.ChangeTracker.DetectChanges();
        Console.WriteLine("After add the post/tag relation:");
        Console.WriteLine(context.ChangeTracker.DebugView.LongView);
        Console.WriteLine();

        #endregion

        //New code.
        context.SaveChanges();

        context.ChangeTracker.DetectChanges();
        Console.WriteLine("After save:");
        Console.WriteLine(context.ChangeTracker.DebugView.LongView);
        Console.WriteLine();

        post.Tags.Remove(tag);
        context.ChangeTracker.DetectChanges();
        Console.WriteLine("After remove the post/tag relation:");
        Console.WriteLine(context.ChangeTracker.DebugView.LongView);
        Console.WriteLine();

        post.Tags.Add(tag); //This is not working, relation state is still deleted.
        context.ChangeTracker.DetectChanges();
        Console.WriteLine("After add it again:");
        Console.WriteLine(context.ChangeTracker.DebugView.LongView);
        Console.WriteLine();
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants