GraphDiff icon indicating copy to clipboard operation
GraphDiff copied to clipboard

Associated Collection reference being update with parent

Open igormpmartins opened this issue 2 years ago • 0 comments
trafficstars

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:,

  • User class has an ICollection of Team.
  • Team has a property named CreatedBy (of type User).
  • After using UpdateGraph to add a collection of Teams to a certain user (user.Teams), every Team.CreatedBy is updated with the user that I'm using as parent node.

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.

igormpmartins avatar Mar 24 '23 19:03 igormpmartins