private void btnSignUp_Click(object sender, EventArgs e) { using (var ctx = new UsersContext()) { var login = MetaBackEnd.getCorrectLogin(tbLogin.Text); var rawpass = MetaBackEnd.getCorrectLogin(tbPass.Text); var birthDate = dtpBirthDate.Value; var signUpDate = DateTime.Now; var idSpecial = ctx.specials.SingleOrDefault(spec => tbSpecial.Text == spec.special_code).id; var bsalt = MetaBackEnd.GenerateSalt(); var bpass = MetaBackEnd.CalcHash(rawpass, bsalt); var newUser = new awp_users { gender = genderName, login_ = login, password = bpass, birth_date = birthDate, signup_date = signUpDate, salt = bsalt, id_special = idSpecial }; ctx.awp_users.Add(newUser); ctx.SaveChanges(); } }
private void DeleteRecordWithCtx(UsersContext ctx, string table, object deletedObj) { // Question1!!! Ужос, везде код дублируется. Alt+shift+> помогает switch (table) // Exlicipt casting to correct class type { case "awp_users": { awp_users delObj = (awp_users)deletedObj; ctx.awp_users.Attach(delObj); ctx.awp_users.Remove(delObj); break; } case "specials": { special delObj = (special)deletedObj; ctx.specials.Attach(delObj); ctx.specials.Remove(delObj); break; } default: throw new Exception("ERROR\n in DeleteRecordWithCtx, wrong case "); } }
private void UpdateRecordWithCtx(UsersContext ctx, object updatedObj, string tableName) { // QUESTION как это исправить всё? switch (tableName) // Exlicipt casting to correct class type { case "awp_users": { awp_users updObj = (awp_users)updatedObj; // create updated awp_users var oldObj = ctx.awp_users.SingleOrDefault(o => o.id == updObj.id); ctx.Entry(oldObj).CurrentValues.SetValues(updatedObj); // get entity's values and update the values break; } case "specials": { special updObj = (special)updatedObj; var oldObj = ctx.specials.SingleOrDefault(o => o.id == updObj.id); ctx.Entry(oldObj).CurrentValues.SetValues(updatedObj); break; } default: throw new Exception("ERROR\n in UpdateRecordWithCtx, wrong case "); } }
private object GetCorrectObj(DataGridView dgv, int row, string dataBase) // return correct class instance { var curRowCells = dgv.Rows[row].Cells; var idFromFirstCell = curRowCells[0].Value == null ? 0 : (int)curRowCells[0].Value; // Cell can be null...... switch (dataBase) { case "awp_users": { var obj = new awp_users { id = idFromFirstCell, login_ = (string)curRowCells[1].Value, password = (byte[])curRowCells[2].Value, salt = (byte[])curRowCells[2].Tag, id_special = (int)curRowCells[3].Value, gender = (string)curRowCells[4].Value, signup_date = (DateTime)curRowCells[5].Value, birth_date = (DateTime)curRowCells[6].Value }; return(obj); } case "specials": { var obj = new special { id = idFromFirstCell, special_code = (string)curRowCells[1].Value, id_role = (int)curRowCells[2].Value }; return(obj); } default: throw new Exception("Error in GetCorrectObj method"); } }