C# context is already tracking the entity, azure a

2019-08-09 06:16发布

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

http://blogs.msdn.com/b/aadgraphteam/archive/2014/12/12/announcing-azure-ad-graph-api-client-library-2-0.aspx

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();
        }

1条回答
Rolldiameter
2楼-- · 2019-08-09 07:12

Hey everyone I found what is issue now in this. it was the bug on the old Graph client library. it is solved in the Graph client library 2.0, So please use Graph client library 2.0 or higher.

This is the link where you can download latest graph client library: https://www.nuget.org/packages/Microsoft.Azure.ActiveDirectory.GraphClient/

查看更多
登录 后发表回答