示例#1
0
 public void Save()
 {
     _context.SaveChanges();
 }
        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);
        }