I'm new to Identity, EF6, and SQL databases. Please bear with me.
The idea is this: For each user in the database, change a user variable, and save that user. However, I must be going about it incorrectly, as I trip an InvalidOperationException (the infamous: Attaching an entity of type 'Models.ApplicationUser' failed because another entity of the same type already has the same primary key value.)
var _db = new ApplicationDbContext();
IQueryable<ApplicationUser> query = _db.Users; // from IdentityDbContext
// search for matching user
List<ApplicationUser> subordinates = query.Where(p => (p.ParentId == userEmployeeId)).ToList();
if (subordinates != null)
{
foreach (var sub in subordinates)
{
sub.Nickname = "Scooby"; // change an example test variable
IdentityResult result = manager.Update(sub); // EXCEPTION here! Boom.
if (!result.Succeeded)
{
Console.WriteLine("User did not save");
}
}
}
Q: What am I doing wrong here? Or, how should I attack this differently?