C#上下文已经跟踪实体,蔚蓝的活动目录图形API。添加组成员 [英] C# context is already tracking the entity, azure active directory graph api. add group member

查看:331
本文介绍了C#上下文已经跟踪实体,蔚蓝的活动目录图形API。添加组成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图添加成员到Azure Active Directory组,但它失败,显示下面的错误我。


  

上下文已经跟踪实体


我试图找到很多关于它,我也看到了这个链接

<一个href=\"http://stackoverflow.com/questions/27443449/azure-active-directory-graph-client-2-0-context-is-not-currently-tracking-the\">Azure Active Directory的客户端图表2.0 - 上下文当前没有跟踪实体

<一个href=\"http://blogs.msdn.com/b/aadgraphteam/archive/2014/12/12/announcing-azure-ad-graph-api-client-library-2-0.aspx\" rel=\"nofollow\">http://blogs.msdn.com/b/aadgraphteam/archive/2014/12/12/announcing-azure-ad-graph-api-client-library-2-0.aspx

但我没有得到任何成功,请帮助我。

这是我的code:

 尝试
        {
            ActiveDirectoryClient客户= ADGraphHelper.GetActiveDirectoryClient();
            IGroupFetcher groupFetcher = client.Groups.GetByObjectId(的groupId);
            群组=(集团)(等待groupFetcher.ExecuteAsync());
            串[]用户id = userId.Split(,);
            的foreach(在用户id字符串ID)
            {
                如果(ID!=)
                {
                    IUSER用户= client.Users.Where(U =&GT; u.ObjectId ==标识)。.Exe​​cuteAsync()Result.CurrentPage.ToArray()第一();
                    如果(用户!= NULL)
                    {
                        //检查水灰比用户已位于present成组或不
                        IDirectoryObject userExists = group.Members.Where(U =&GT; u.ObjectId == ID).FirstOrDefault();
                        如果(userExists == NULL)
                        {
                            group.Members.Add(用户为DirectoryObject);
                        }
                    }
                    其他
                        抛出新的异常(用户为空);
                }
            }
            等待group.UpdateAsync();
            返回JSON(新{成功=真});
        }
        赶上(异常前)
        {
            ModelState.AddModelError(,我们粗野,处理您的请求,请联系以获取更多详细信息支持。);
//错误处理code。
            返回PartialView();
        }


解决方案

大家好,我发现有什么问题,现在在这个。它是在旧的图形客户端库的bug。它在图形客户端库2.0解决了,所以请使用图形客户端库2.0或更高版本。

这是在这里你可以下载最新的图形客户端库的链接:
https://www.nuget.org/packages/Microsoft.Azure.ActiveDirectory。 GraphClient /

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

解决方案

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/

这篇关于C#上下文已经跟踪实体,蔚蓝的活动目录图形API。添加组成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆