Azure Active Directory Graph Client 2.0 - Context is not currently tracking the entity
Asked Answered
C

3

2

I have recently installed the Azure Active Directory Graph Client Library 2.0.2 Nuget package and am unable to add Members to Groups both adding a group to a group or adding a user to a group I am getting the following error when the AddLink function is called:

“[System.InvalidOperationException] = {"The context is not currently tracking the entity."}

My code:

IGroup group = azureClient.Groups.GetByObjectId("Guid here").ExecuteAsync().Result;
IGroup groupToAdd = azureClient.Groups.GetByObjectId("Guid here").ExecuteAsync().Result;
azureClient.Context.AddLink(group, "Members", groupToAdd);
azureClient.Context.SaveChanges();

I have been unable to find any mention of this error in relation to the Azure Active Directory Graph Client Library from doing Google searches so any help on this would be much appreciated.

Conference answered 12/12, 2014 at 12:13 Comment(0)
H
5

We’ve just released an update to the Graph client library, that fixes this problem. You should now be able to add members to groups. The mechanism is a little different from using AddLinks (and hopefully simpler).

We also have a new blog describing the client library which talks about this and many other things: http://blogs.msdn.com/b/aadgraphteam/archive/2014/12/12/announcing-azure-ad-graph-api-client-library-2-0.aspx

For reference, to add a member to a group:

{groupObject}.Members.Add({entityObject} as DirectoryObject);

So for example, to update a group with a new user member:

myGroup.Members.Add(userToBeAdded as DirectoryObject); await myGroup.UpdateAsync();

NOTE: The same construct can be used to add users to a DirectoryRole object. Groups and users may be added to a group, however, for now, only users can be added to a DirectoryRole.

Hope this helps,

Hexachlorophene answered 22/12, 2014 at 20:36 Comment(1)
I tried to do same in the blog. but it fails and showing me following error : context is already tracking the entity, You can see code in my this question : #34965013Eleaseeleatic
C
2

I had the same issue, and the documentation wasn't very clear, so maybe this will help others. You cannot add users as members of an IGroup, but only to a Group. You also cannot add IDirectoryObjects to the Members collection, but only DirectoryObjects. You must first cast your IUser and IGroup objects. The following code is what I have working at the moment:

var graphClient = new ActiveDirectoryClient(new Uri(ConfigHelper.GraphServiceRoot), async () => await GetUserTokenAsync(cache));
var actualUser = await graphClient.Users.GetByObjectId(matchedUser.ObjectId).ExecuteAsync();
var actualGroup = (Group) await graphClient.Groups.GetByObjectId(matchedGroup.ObjectId).ExecuteAsync();

actualGroup.Members.Add(actualUser as DirectoryObject);
await graphClient.Context.SaveChangesAsync();
Cucurbit answered 20/10, 2015 at 1:34 Comment(0)
O
0

I tried this new syntax but still does not work.

 public async Task<Result<dynamic>> addUserToAzureGroup(Group AzGroup, User AzUser)
 {
     // link the found user with the found group
     try
     {
         AzGroup.Members.Add(AzUser as DirectoryObject);
         await AzGroup.UpdateAsync();
     }
     catch (Exception ex)
     {
         Exception myEx = new Exception(ex.Message);
         retResult.Exception = myEx;
         return retResult;
     }
     return retResult;
 }

I have almost the same error text in the execption message: The context is already tracking the relationship

Any news on that issue? Could anyone guess why that happens?

I also tried from the manage.windowsAzure.com UI and still cannot add the user! I get this error: Could not add members to group 'myAzAD_group'.

Onomasiology answered 31/3, 2015 at 13:5 Comment(2)
Seems whole Azure was stuck at the time. It worked the next day without any changesOnomasiology
In this type of case before one day you should have old library of directory client after day you should update the api nuget package.Eleaseeleatic

© 2022 - 2024 — McMap. All rights reserved.