Skip to content

Same List instance is returned for empty arrays #3417

Closed

Description

@jrhodnik

I'm running into an issue where when a model includes multiple List<T>s, such a List<string> or a List<int>, and I add a new item to the List<T>, that same new item is added to all List<T>s with the same T.

For example, if you have a model such as this:

    public class TestModel
    {
        public Guid Id { get; set; }
        public List<string> PropOne { get; set; } = new List<string>();

        public List<int> PropTwo { get; set; } = new List<int>();
        public List<int> PropThree { get; set; } = new List<int>();
        public List<int> PropFour { get; set; } = new List<int>();
    }

and you add an item to PropFour, both PropThree and PropTwo get the same item added. Note that PropOne will be unaffected as it is a different type. I have a fully functional example of this issue in this repository. Hopefully this isn't one of those instances where it's unproducible on other machines :).

Activity

  1. roji commented on Dec 18, 2020

    @roji
    Member

    @jrhodnik I can see the odd behavior - thanks for reporting this! I've opened dotnet/efcore#23726 as this is reproducible on EF Core regardless of PostgreSQL.

  2. transferred this issue fromnpgsql/efcore.pgon Dec 18, 2020
  3. self-assigned this
    on Dec 18, 2020
  4. added this to the 5.0.2 milestone on Dec 18, 2020
  5. roji commented on Dec 18, 2020

    @roji
    Member

    This turned out to be an ADO.NET-level bug - we return the same instance when an empty List<T> is returned.

  6. reopened this on Dec 18, 2020
  7. changed the title [-]Model List<T> Additions Leaking To Other List<T>s[/-] [+]Same List instance is returned for empty arrays[/+] on Dec 18, 2020
  8. roji commented on Dec 18, 2020

    @roji
    Member

    @jrhodnik thanks again for flagging this - this will be fixed fox 5.0.2. In the meantime, rather than mutating the List on the loaded entity instance, replace it with a new List which contains the new contents.

  9. added a commit that references this issue on Dec 18, 2020
    14495f9
  10. added 2 commits that reference this issue on Dec 19, 2020
    f788246
    06fc027
  11. roji commented on Dec 19, 2020

    @roji
    Member

    Note: this bug isn't present in 4.1 (introduced in c95b6ec)

  12. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions