ID为{0}的用户不存在,

时间:2017-06-01 06:46:27

标签: c# sql-server visual-studio asp.net-web-api

我正在开发一个使用WebAPI和IntegrationApI Provider项目的学习管理系统。在运行解决方案时,会出现以下错误 Screen shot 这是出现的例外情况。我无法理解这个异常,无法通过互联网找到解决方案。请帮忙

CODE: 命名空间IntegrationAPIProviders {     公共类TestIntegrationAPIProvider2:IPlatform     {

    public ID2LAppContext valenceAppContext { get; set; }
    public HostSpec valenceHost { get; set; }
    public ID2LUserContext valenceUserContext { get; set; }
    public ID2LUserContext valenceAdminContext { get; set; }

    private int _CurrentUserId;
    public int GetCurrentUserId()
    {
        if (_CurrentUserId == 0)
        {
            var allUsers = UserRepository.GetAllUsers();

            if (allUsers != null && allUsers.Count>0)
                _CurrentUserId = allUsers.First().UserId;
            else
                _CurrentUserId = 1;
        }

        return _CurrentUserId;
    }

    public void SetCurrentUserId(int userId)

    {
        _CurrentUserId = userId;
    }

    public PlatformUser GetUserName(int userId)
    {
        var user = UserRepository.GetUserById(userId);
        if (user == null)
            throw new Exception(string.Format("user with id:{0} does not exist, please add the user first", userId));

        return new PlatformUser() { UserName = user.Name, Id = userId };

        // create a proxy object of web service 
        // invoke that web serice
        // rteurn value

        // coonect to db
        // execute the sql
        // rteurn the result

        //return AllUsersList.Single(x => x.Id == userId);

        //return new PlatformUser() { Id = userId, FirstName = "Student", LastName = "0", ExternalEmail = "bsuser1@brightscribe.com" };
    }

    public List<PlatformUser> GetUserIdsForCourse(int courseId)
    {
        var users = UserRepository.GetAllUsersByCourseId(courseId);

        return users.Select(x => new PlatformUser() { Id = x.UserId, FirstName = x.Name, UserName = x.Name }).ToList();

        //var userList = AllUsersList.Where(x => x.LastName.Contains(courseId.ToString())).ToList();
        //userList.AddRange(AllUsersList.Where(x => x.LastName.Equals("ALL", StringComparison.CurrentCultureIgnoreCase)));
        //return userList;
    }

    List<PlatformUser> IPlatform.GetAllUsers(int courseId)
    {
        return AllUsersList;
    }

    public List<PlatformUser> AllUsersList
    {
        get
        {
            var users = UserRepository.GetAllUsers();
            return users.Select(x => new PlatformUser() { Id = x.UserId, FirstName = x.Name, UserName = x.Name }).ToList();

            //return new List<PlatformUser>()
            //{
            //new PlatformUser() {Id=0, FirstName = "Student 00", LastName = "1,2", ExternalEmail = "bsuser0@brightscribe.com" },
            //new PlatformUser() {Id=1, FirstName = "Student 01", LastName = "1,2,3", ExternalEmail = "bsuser1@brightscribe.com" },
            //new PlatformUser() {Id=2, FirstName = "Student 02", LastName = "2,3", ExternalEmail = "bsuser2@brightscribe.com" },
            //new PlatformUser() {Id=3, FirstName = "Student 03", LastName = "1,3", ExternalEmail = "bsuser3@brightscribe.com" },
            //new PlatformUser() {Id=4, FirstName = "Student 04", LastName = "3", ExternalEmail = "bsuser12@brightscribe.com" },
            //new PlatformUser() {Id=5, FirstName = "Student 05", LastName = "2", ExternalEmail = "bsuser13@brightscribe.com" },
            //new PlatformUser() {Id=6, FirstName = "Student 06", LastName = "1", ExternalEmail = "bsuser14@brightscribe.com" },
            //new PlatformUser() {Id=7, FirstName = "Student 07", LastName = "1,3", ExternalEmail = "bsuser15@brightscribe.com" },
            //new PlatformUser() {Id=8, FirstName = "Developer 08", LastName = "ALL", ExternalEmail = "bsuser4@brightscribe.com" },
            //new PlatformUser() {Id=9, FirstName = "Developer 09", LastName = "ALL", ExternalEmail = "bsuser5@brightscribe.com" },
            //new PlatformUser() {Id=10, FirstName = "Developer 10", LastName = "ALL", ExternalEmail = "bsuser6@brightscribe.com" },
            //new PlatformUser() {Id=11, FirstName = "Instructor 11", LastName = "ALL", ExternalEmail = "bsuser7@brightscribe.com" },
            //new PlatformUser() {Id=12, FirstName = "Instructor 12", LastName = "ALL", ExternalEmail = "bsuser8@brightscribe.com" },
            //new PlatformUser() {Id=13, FirstName = "Instructor 13", LastName = "ALL", ExternalEmail = "bsuser9@brightscribe.com" },
            //new PlatformUser() {Id=14, FirstName = "Admin 14", LastName = "ALL", ExternalEmail = "bsuser10@brightscribe.com" },
            //new PlatformUser() {Id=15, FirstName = "Manager 15", LastName = "ALL", ExternalEmail = "bsuser11@brightscribe.com" }
            //};
        }
    }

    public List<PlatformCourse> AllCourses
    {
        get
        {
            var courses = CourseManager.GetAllCourse();

            return courses.Select(x => new PlatformCourse() { Id = x.CourseId, CourseName = x.Name }).ToList();
            //return new List<PlatformCourse>()
            //{
            //    new PlatformCourse(){ Id = 1, CourseName = "Course1"},
            //    new PlatformCourse(){ Id = 2, CourseName = "Course2"},
            //    new PlatformCourse(){ Id = 3, CourseName = "Course3"},
            //};
        }
    }

    public List<PlatformCourse> GetCoursesForUser(int userId)
    {
        //TODO: need a filter so that only the courses for the users are obtained.
        var courses = CourseManager.GetAllCourse();
        return courses.Select(x => new PlatformCourse() { Id = x.CourseId, CourseName = x.Name }).ToList();

        //var user = AllUsersList.Where(x => x.Id == userId).Single();
        //if (user.LastName.StartsWith("ALL"))
        //    return this.AllCourses;

        //List<string> courseIds = user.LastName.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();

        //List<PlatformCourse> userCourseList = new List<PlatformCourse>();
        //foreach (var requiredCourseId in courseIds)
        //{
        //    userCourseList.Add(AllCourses.Where(x => x.Id.ToString().Equals(requiredCourseId)).Single());
        //}

        //return userCourseList;
    }

    public PlatformUserRole GetUserRole(int courseId, int userId)
    {
        //UserEnrollment userEnrolled = UserRepository.GetUserEnrollmentById(userId, courseId);
        //return (userEnrolled == null) ? PlatformUserRole.None : (PlatformUserRole)userEnrolled.UserRole;


        var user = UserRepository.GetUserById(userId);
        if (user == null)
            return PlatformUserRole.Admin;

        if (!string.IsNullOrEmpty(user.Phone))
        {
            if (user.Phone.Contains("StudentServices"))
                return PlatformUserRole.StudentServices;

            if (user.Phone.Contains("Student"))
                return PlatformUserRole.Student;

            if (user.Phone.Contains("Developer"))
                return PlatformUserRole.Developer;

            if (user.Phone.Contains("Instructor"))
                return PlatformUserRole.Instructor;

            if (user.Phone.Contains("Manager"))
                return PlatformUserRole.Manager;

            if (user.Phone.Contains("Admin"))
                return PlatformUserRole.Admin;

            if (user.Phone.Contains("Employer"))
                return PlatformUserRole.Employer;
        }

        if (user.Name.Contains("StudentServices"))
            return PlatformUserRole.StudentServices;
        else if (user.Name.Contains("Student"))
            return PlatformUserRole.Student;
        else if (user.Name.Contains("Admin"))
            return PlatformUserRole.Admin;
        else if (user.Name.Contains("Applicant"))
            return PlatformUserRole.Applicant;
        else if (user.Name.Contains("Developer"))
            return PlatformUserRole.Developer;
        else if (user.Name.Contains("Employer"))
            return PlatformUserRole.Employer;
        else if (user.Name.Contains("Instructor"))
            return PlatformUserRole.Instructor;
        else if (user.Name.Contains("Manager"))
            return PlatformUserRole.Manager;

        else if (user.Name.Contains("Instructor"))
            return PlatformUserRole.Instructor;
        else if (user.Name.Contains("Instructor"))
            return PlatformUserRole.Instructor;
        else
        {
            return PlatformUserRole.Admin;
        }

        //switch (userId)
        //{
        //    case 0:
        //    case 1:
        //    case 2:
        //    case 3:
        //    case 4:
        //    case 5:
        //    case 6:
        //    case 7:
        //        role = PlatformUserRole.Student;
        //        break;
        //    case 8:
        //    case 9:
        //    case 10:
        //        role = PlatformUserRole.Developer;
        //        break;
        //    case 11:
        //    case 12:
        //    case 13:
        //        role = PlatformUserRole.Instructor;
        //        break;
        //    case 14:
        //        role = PlatformUserRole.Admin;
        //        break;
        //    case 15:
        //        role = PlatformUserRole.Manager;
        //        break;
        //    default:
        //        break;
        //}
        //return role;
    }

    public bool UpdateGradeData(int courseId, int userId, decimal grade, int d2lGradeId)
    {
        Trace.WriteLine(string.Format("UpdateGradeData invoked for , UserId:{0}, Grade:{1}, d2l:{2}", userId, grade, d2lGradeId));
        return true;
    }

    public List<PlatformTrigger> GetTriggers(int courseId)
    {
        return new List<PlatformTrigger>()
        {
            new PlatformTrigger(){ Id=0, ModuleId="TriggerModule0"},
            new PlatformTrigger(){ Id=1, ModuleId="TriggerModule1"},
            new PlatformTrigger(){ Id=2, ModuleId="TriggerModule2"},
            new PlatformTrigger(){ Id=3, ModuleId="TriggerModule3"},
            new PlatformTrigger(){ Id=4, ModuleId="TriggerModule4"},
            new PlatformTrigger(){ Id=5, ModuleId="TriggerModule5"}
        };
    }


    public bool CheckTriggers(int courseId, int userid, int d2LTriggerId)
    {
        return true;
    }


    public List<PlatformUser> GetAllInstructors(int courseId)
    {
        var courseUsers = GetUserIdsForCourse(courseId);
        return courseUsers.Where(x => GetUserRole(courseId, x.Id) == PlatformUserRole.Instructor).ToList();
        //List<PlatformUser> alUsers = PlatformFactory.CreateProvider().GetUserIdsForCourse(courseId);
        //List<PlatformUser> instructorsList = new List<PlatformUser>();
        //foreach (var user in alUsers)
        //{
        //    PlatformUserRole userRole = PlatformFactory.CreateProvider().GetUserRole(courseId, user.Id);
        //    if (userRole == PlatformUserRole.Instructor)
        //        instructorsList.Add(user);
        //}

        //return instructorsList;
    }

    public bool TranscriptionsCompleted(int courseId, int userId, int d2lNextChapterId)
    {
        Trace.WriteLine(string.Format("TranscriptionsCompleted invoked for , CourseId:{0}, UserId:{1}, d2lNextChapterId:{2}", courseId, userId, d2lNextChapterId));
        return true;
    }

    public bool TranscriptionsIncomplete(int courseId, int userId, int d2LNextChapterId)
    {
        throw new NotImplementedException("only available in brightspace API");
    }

    public EnrollmentData API_GetUserEnrollmentById(int userId, int orgUnitId)
    {
        throw new NotImplementedException("only available in brightspace API");
    }

    public List<TestDetail> GetTestCompletionDates(int courseId, int userId)
    {
        return new List<TestDetail>()
        {
            new TestDetail(){ Grade = 1.1m, GradeWeight = 1.11m, TestId = 1, TestName = "Test1", TestPassed = false },
            new TestDetail(){ Grade = 2.2m, GradeWeight = 2.22m, TestId = 2, TestName = "Test2", TestPassed = false}
        };
    }

    public GradeValue GetGradeValue(int courseId, int userId, int gradeId)
    {
        throw new NotImplementedException("only available in brightspace API");
    }


    public List<DropboxFolder> GetDropboxFolders(int courseId)
    {
        throw new NotImplementedException("only available in brightspace API");
    }

    public List<EntityDropbox> GetDropboxFolderSubmissions(int courseId, int id)
    {
        throw new NotImplementedException("only available in brightspace API");
    }

    public GradeValue GetFinalGradeValue(int courseId, int userId, int gradeId)
    {
        throw new NotImplementedException("only available in brightspace API");
    }

    public MemoryStream GetDropboxFolderSubmissionFile(int courseId, int folderId, int submissionId, int fileId)
    {
        throw new NotImplementedException("only available in brightspace API");
    }


}

}

1 个答案:

答案 0 :(得分:1)

图片中的代码执行以下操作:

  • 使用给定的UserRepository.GetUserById
  • 调用方法userId
  • 检查该通话是否返回了结果
  • 如果没有返回结果会抛出一个错误,告诉您具有该给定id的用户不存在

由于您是开发人员,我不需要向您解释; - )

要解决此问题,请检查userId的内容并检查您的数据源是否存在该ID