默认情况下,当您获取某个实体(在我们的示例中为“用户”)时,不会加载所有导航属性。您应该使用 Expand(..)。下面我举例说明如何将 Manager 分配给 User 以及如何获取 User 的 Manager 导航属性(省略了一些基础结构的细微差别):
public async Task<Result> AssingUserManager(string userUpn, string managerUpn)
{
try
{
var user = (AD.User)await ADClient.Users
.Where(x => x.UserPrincipalName == userUpn)
.ExecuteSingleAsync();
var manager = (AD.User)await ADClient.Users
.Where(x => x.UserPrincipalName == managerUpn)
.ExecuteSingleAsync();
user.Manager = manager;
await manager.UpdateAsync();
return Result.Ok();
}
catch (Exception ex)
{
return Result.Fail(new Error("Failed to assign manager", null, ex));
}
}
public async Task<Result<User>> GetUserManager(string upn)
{
try
{
var user = (AD.User)await ADClient.Users
.Where(x => x.UserPrincipalName == upn)
.Expand(x => x.Manager)
.ExecuteSingleAsync();
var manager = user.Manager as AD.User;
if (manager != null)
{
return Result.Ok(ConvertToModel(manager));
}
return Result.Fail<User>(new Error("Manager not found for specified user", null));
}
catch (Exception ex)
{
return Result.Fail<User>(new Error("Failed to get user's manager", null, ex));
}
}
另外,请注意,当我更新图表时,我通过对被分配为经理的用户而不是正在设置 Manager 属性的用户调用 UpdateAsync 来做到这一点(取自此来源)