zzzprojects / GraphDiff

GraphDiff is a library that allows the automatic update of a detached graph using Entity Framework code first.
https://entityframework-graphdiff.net/overview
MIT License
333 stars 101 forks source link

Associated Collection reference being update with parent #187

Open igormpmartins opened 1 year ago

igormpmartins commented 1 year ago

1. Description

After using UpdateGraph, if an associated collection has an attribute from the same class as the parent node, this attribute ends up being updated with a reference to the parent node.

The scenario is:,

Here is the code snippet for reproducing the problem:

var user = context.Users.AsNoTracking().FirstOrDefault(q => q.Name == "User_A");
var teams = context.Teams.AsNoTracking().Where(q => q.Name == "Team A").ToList();
user.Teams = teams;
context.UpdateGraph(user, map => map.AssociatedCollection(q => q.Teams));
context.SaveChanges();

So, after using UpdateGraph/SaveChanges, "Team A".CreatedBy receives "User A", although I didn't specify that.

Here are the classes used on this example:

    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Boolean IsActive { get; set; }
        public ICollection<Team> Teams {get; set; }
    }

    public class Team 
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public User CreatedBy { get; set; }
    }

2. Exception

No exception occurs, just the behavior isn't the expected one.

3. Fiddle

I have a Fiddle that reproduces the problem: https://dotnetfiddle.net/iaC9MW

4. Any further technical details

The fiddle reproduces the exact same problem that I have on my project.