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