public bool DeleteStudent(string emailAddress)
        {
            //tblStudent student = _context.tblStudent.Find(EmailAddress);
            tblStudent removedStudent = (from student in _context.tblStudent
                                         where student.EmailAddress == emailAddress
                                         select student).FirstOrDefault();

            var removedStudentHistory = from studentHistory in _context.tblStudentHistory
                                        where studentHistory.StudentID == removedStudent.StudentID
                                        select studentHistory;

            tblUser removedUser = (from user in _context.tblUser
                                   where user.EmailAddress == removedStudent.EmailAddress
                                   select user).FirstOrDefault();

            //tblStudent removedStudent = getStudent;

            //Removing Student
            _context.Entry(removedStudent).State = System.Data.Entity.EntityState.Deleted;

            //Removing Student's History
            foreach (var entry in removedStudentHistory)
            {
                _context.Entry(entry).State = System.Data.Entity.EntityState.Deleted;
            }

            //Removing the user for that student
            _context.Entry(removedUser).State = System.Data.Entity.EntityState.Deleted;

            //_context.tblStudentHistory.Remove(removedStudentHistory);
            //_context.tblStudent.Remove(removedStudent);
            //_context.tblUser.Remove(removedUser);

            _context.SaveChanges();

            //if ()
            return(true);
            //else
            //    return false;
        }
        public bool UpdateStudent(StudentModel studentModel)
        {
            //_context.Entry(studentModel).State = EntityState.Modified;
            tblStudent newStudent = new tblStudent();

            newStudent = _context.tblStudent.FirstOrDefault(i => i.StudentID == studentModel.StudentID);
            //newStudent = _context.tblStudent.Find(studentModel.StudentID);

            if (newStudent.Equals(null))
            {
                return(false);
            }

            //Adding to Student Info
            newStudent.FirstName     = studentModel.FirstName;
            newStudent.LastName      = studentModel.LastName;
            newStudent.ContactNumber = studentModel.ContactNumber;
            newStudent.EmailAddress  = studentModel.EmailAddress;
            newStudent.DateOfBirth   = studentModel.DateOfBirth;
            newStudent.Address       = studentModel.Address;
            newStudent.CityID        = studentModel.CityID;

            var newStudentHistory = from studentHistoryAll in _context.tblStudentHistory
                                    where studentModel.StudentID == studentHistoryAll.StudentID
                                    select studentHistoryAll;

            bool entryNotExists = true;

            foreach (var entry in newStudentHistory)
            {
                if (studentModel.CollegeID.Equals(entry.CollegeID) &&
                    studentModel.DepartmentID.Equals(entry.DepartmentID) &&
                    studentModel.DateOfJoining.Equals(entry.DateOfJoining) &&
                    studentModel.Semester.Equals(entry.Semester))
                {
                    entryNotExists = false;
                    break;
                }
            }


            ////New entry in student history table if any changes in its fields
            //if(!studentModel.CollegeID.Equals(newStudentHistory.CollegeID)
            //    || !studentModel.DepartmentID.Equals(newStudentHistory.DepartmentID)
            //    || !studentModel.Semester.Equals(newStudentHistory.Semester)
            //    || !studentModel.DateOfJoining.Equals(newStudentHistory.DateOfJoining))
            //{

            if (entryNotExists)
            {
                Guid newStudentHistoryID = Guid.NewGuid();
                tblStudentHistory newStudentHistoryEntry = new tblStudentHistory();
                newStudentHistoryEntry.StudentHistoryID = newStudentHistoryID;
                newStudentHistoryEntry.CollegeID        = studentModel.CollegeID;
                newStudentHistoryEntry.DepartmentID     = studentModel.DepartmentID;
                newStudentHistoryEntry.Semester         = studentModel.Semester;
                newStudentHistoryEntry.DateOfJoining    = studentModel.DateOfJoining;
                newStudentHistoryEntry.StudentID        = studentModel.StudentID;

                _context.tblStudentHistory.Add(newStudentHistoryEntry);
            }

            _context.SaveChanges();

            return(true);
        }
        public bool InsertStudent(StudentModel studentModel)
        {
            var checkIfStudentExists = from studentCheck in _context.tblStudent
                                       where studentCheck.StudentCode == studentModel.StudentCode
                                       select studentCheck;

            if (checkIfStudentExists.Any())
            {
                return(false);
            }

            Guid newGuid = Guid.NewGuid();

            //var getStudentsCityId = (from studentCity in _context.tblCity
            //                        where studentCity.CityName == studentModel.CityName
            //                        select studentCity.CityID).FirstOrDefault();

            tblStudent student = new tblStudent()
            {
                StudentID     = newGuid,
                StudentCode   = studentModel.StudentCode,
                FirstName     = studentModel.FirstName,
                LastName      = studentModel.LastName,
                ContactNumber = studentModel.ContactNumber,
                EmailAddress  = studentModel.EmailAddress,
                DateOfBirth   = studentModel.DateOfBirth,
                Address       = studentModel.Address,
                CityID        = studentModel.CityID
            };

            //var getStudentCollegeID = (from studentCollege in _context.tblCollege
            //                            where studentCollege.CollegeName == studentModel.CollegeName
            //                            select studentCollege.CollegeID).FirstOrDefault();

            //var getStudentDepartmentID = (from studentDept in _context.tblDepartment
            //                               where studentDept.DepartmentName == studentModel.DepartmentName
            //                               select studentDept.DepartmentID).FirstOrDefault();
            Guid newGuid2 = Guid.NewGuid();
            tblStudentHistory studentHistory = new tblStudentHistory()
            {
                StudentHistoryID = newGuid2,
                DepartmentID     = studentModel.DepartmentID,
                CollegeID        = studentModel.CollegeID,
                Semester         = studentModel.Semester,
                DateOfJoining    = studentModel.DateOfJoining,
                StudentID        = newGuid,
            };

            Guid newUserID = Guid.NewGuid();

            //Generating a new MD5 encrypted password
            MD5    md5Hash              = MD5.Create();
            string newPassword          = "******";
            string newEncryptedPassword = Encryptor.GetMd5Hash(md5Hash, newPassword);

            tblUser newStudentUser = new tblUser()
            {
                UserID       = newUserID,
                EmailAddress = studentModel.EmailAddress,
                Password     = newEncryptedPassword,
                RoleID       = 3
            };


            _context.tblStudent.Add(student);
            _context.tblStudentHistory.Add(studentHistory);
            _context.tblUser.Add(newStudentUser);

            _context.SaveChanges();

            return(true);
        }