I am trying to add member to azure active directory group but it fails it showing me following error.
context is already tracking the entity
I tried to found lot for it, I also see this links
Azure Active Directory Graph Client 2.0 - Context is not currently tracking the entity
But I not get any success please help me.
This is my code :
try
{
ActiveDirectoryClient client = ADGraphHelper.GetActiveDirectoryClient();
IGroupFetcher groupFetcher = client.Groups.GetByObjectId(groupId);
Group group = (Group)(await groupFetcher.ExecuteAsync());
string[] userIds = userId.Split(',');
foreach (string id in userIds)
{
if (id != "")
{
IUser user = client.Users.Where(u => u.ObjectId == id).ExecuteAsync().Result.CurrentPage.ToArray().First();
if (user != null)
{
//check wather user aleady present into group or not
IDirectoryObject userExists = group.Members.Where(u => u.ObjectId == id).FirstOrDefault();
if (userExists == null)
{
group.Members.Add(user as DirectoryObject);
}
}
else
throw new Exception("User is null.");
}
}
await group.UpdateAsync();
return Json(new { success = true });
}
catch (Exception ex)
{
ModelState.AddModelError("", "we connot process your request please contact to support for more details.");
// error handling code.
return PartialView();
}