C# 上下文已经在跟踪实体、azure Active Directory 图形 api。添加群组成员

标签 c# azure c#-4.0 azure-active-directory active-directory-group

我正在尝试将成员添加到 azure Active Directory 组,但失败并显示以下错误。

context is already tracking the entity

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

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

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

这是我的代码:

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

最佳答案

大家好,我发现了现在的问题。这是旧 Graph 客户端库上的错误。该问题已在Graph客户端库2.0中解决,因此请使用Graph客户端库2.0或更高版本。

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

关于C# 上下文已经在跟踪实体、azure Active Directory 图形 api。添加群组成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34964513/

相关文章:

c# - 使用 Entity Framework 忽略重复键插入

c# - 仅具有年份 View 的 ASP.NET CalendarExtender

asp.net - 如何防止 ASP.NET 记录身份验证失败?

c# - 向 ScriptCS 添加默认的 using 指令

c# - Web自动化Watin问题

Azure 私有(private) DNS 区域解析

encryption - 为什么我无法在 Azure (Windows Server 2012) 上使用自定义 configProtectionProvider 加密 web.config/appSettings?

sql-server - 在 C# 中从数据库获取字符串值

javascript - 查找用户的位置并将其显示在单个回发的弹出窗口中

c# - 在 C# 中将字符串值格式化为 $xx.xx 格式