private bool Add_Student(Student std, string pass = "") { try { string password = "******"; if (pass.Length > 0) { password = pass; } if (!SettingDatabase.Add_New_Student(std, password)) { MessageBox.Show("Some Error accure to create new user for this student"); return(false); } DbContextDal dal = new DbContextDal(); //User UserStudent = dal.users.Find(std.ID); User UserStudent = dal.users.Find(std.ID); if (UserStudent != null) { std.user = UserStudent; dal.students.Add(std); dal.SaveChanges(); MessageBox.Show("The Student " + std.ID + " Added!"); return(true); } else { MessageBox.Show("Error to add student, because the user of this student deosnt created!"); } } catch (DbEntityValidationException ex) { //MessageBox.Show("Db Entity Validation Exception"); string str = "" + ex.Source + " : " + ex.GetType() + "\n-----------------------------------------------------------------------------------------------------\n" + "Message: " + ex.Message + "\n\nExplain & solution:\nThe problem is that you try to insert an object with data that does not fit the limitations of the data base to these object fields.\n\nThe following is a list of the incorrect features:\n"; foreach (var entityValidationErrors in ex.EntityValidationErrors) { foreach (var validationError in entityValidationErrors.ValidationErrors) { str += ("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage + "\n"); } } MessageBox.Show(str); } catch (DbUnexpectedValidationException ex) { //MessageBox.Show("Db Unexpected Validation Exception"); string str = "" + ex.Source + " : " + ex.GetType() + "\n-----------------------------------------------------------------------------------------------------\n" + "Message: " + ex.Message; MessageBox.Show(str); } catch (DbUpdateException ex) { //MessageBox.Show("Db Update Exception"); string str = "" + ex.Source + " : " + ex.GetType() + "\n-----------------------------------------------------------------------------------------------------\n" + "Message: " + ex.Message + "\n\nExplain & solution:\nThe problem is that you are trying to insert an object that already exists in the system with the same key, or perform object updates with invalid variables in a database. You must enter differentiated data."; MessageBox.Show(str); } catch (InvalidOperationException ex) { //MessageBox.Show("Invalid Operation Exception"); string str = "" + ex.Source + " : " + ex.GetType() + "\n-----------------------------------------------------------------------------------------------------\n" + "Message: " + ex.Message + "\n\nExplain & solution:\nThe problem is that the database is formatted differently from what is currently in your class code. You must do 'add-migration update_i' to changes made during code writing in the class code, and then perform a 'update-database' in your PM."; MessageBox.Show(str); } catch (Exception ex) { //MessageBox.Show("Exception"); string str = "" + ex.Source + " : " + ex.GetType() + "\n-----------------------------------------------------------------------------------------------------\n" + "Message: " + ex.Message; MessageBox.Show(str); } return(false); }
public bool addNewStudent(Student s, string password, string email) { return(SettingDatabase.Add_New_Student(s, password, email)); }