private void UpdateRecordWithCtx(TournamentsContext ctx, object updatedObj, string tableName) { // QUESTION как это исправить всё? switch (tableName) // Exlicipt casting to correct class type { case "knight": { knight updObj = (knight)updatedObj; // create updated knight var oldObj = ctx.knights.SingleOrDefault(o => o.id_knight == updObj.id_knight); ctx.Entry(oldObj).CurrentValues.SetValues(updatedObj); // get entity's values and update the values break; } case "weapon": { weapon updObj = (weapon)updatedObj; var oldObj = ctx.weapons.SingleOrDefault(o => o.id_weapon == updObj.id_weapon); ctx.Entry(oldObj).CurrentValues.SetValues(updatedObj); break; } case "fight": { fight updObj = (fight)updatedObj; var oldObj = ctx.fights.SingleOrDefault(o => o.id_fight == updObj.id_fight); ctx.Entry(oldObj).CurrentValues.SetValues(updatedObj); break; } case "armor": { armor updObj = (armor)updatedObj; var oldObj = ctx.armors.SingleOrDefault(o => o.id_armor == updObj.id_armor); ctx.Entry(oldObj).CurrentValues.SetValues(updatedObj); break; } case "tournament": { tournament updObj = (tournament)updatedObj; var oldObj = ctx.tournaments.SingleOrDefault(o => o.id_tournament == updObj.id_tournament); ctx.Entry(oldObj).CurrentValues.SetValues(updatedObj); break; } default: throw new Exception("ERROR\n in UpdateRecordWithCtx, wrong case "); } }
private static void DeleteRecordWithCtx(TournamentsContext ctx, string table, object deletedObj) { // Question1!!! Ужос, везде код дублируется. Alt+shift+> помогает switch (table) // Exlicipt casting to correct class type { case "knight": { knight delObj = (knight)deletedObj; ctx.knights.Attach(delObj); ctx.knights.Remove(delObj); break; } case "weapon": { weapon delObj = (weapon)deletedObj; ctx.weapons.Attach(delObj); ctx.weapons.Remove(delObj); break; } case "fight": { fight delObj = (fight)deletedObj; ctx.fights.Attach(delObj); ctx.fights.Remove(delObj); break; } case "armor": { armor delObj = (armor)deletedObj; ctx.armors.Attach(delObj); ctx.armors.Remove(delObj); break; } case "tournament": { tournament delObj = (tournament)deletedObj; ctx.tournaments.Attach(delObj); ctx.tournaments.Remove(delObj); break; } default: throw new Exception("ERROR\n in DeleteRecordWithCtx, 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 "knight": { var obj = new knight { id_knight = idFromFirstCell, name = (string)curRowCells[1].Value, birth_date = (DateTime)curRowCells[2].Value, honor = (int)curRowCells[3].Value, id_squire = (int)curRowCells[4].Value }; return(obj); } case "weapon": { var obj = new weapon { id_weapon = idFromFirstCell, name = (string)curRowCells[1].Value, quality = (int)curRowCells[2].Value, id_type = (int)curRowCells[3].Value, id_master = (int)curRowCells[4].Value }; return(obj); } case "fight": { var obj = new fight { id_fight = idFromFirstCell, id_fight_type = (int)curRowCells[1].Value, fight_result = (int)curRowCells[2].Value, id_knight1 = (int)curRowCells[3].Value, id_weapon_knight1 = (int)curRowCells[4].Value, id_armor_knight1 = (int)curRowCells[5].Value, id_knight2 = (int)curRowCells[6].Value, id_weapon_knight2 = (int)curRowCells[7].Value, id_armor_knight2 = (int)curRowCells[8].Value, id_tournament = (int)curRowCells[9].Value }; return(obj); } case "armor": { var obj = new armor { id_armor = idFromFirstCell, name = (string)curRowCells[1].Value, quality = (int)curRowCells[2].Value, id_type = (int)curRowCells[3].Value, id_master = (int)curRowCells[4].Value }; return(obj); } case "tournament": { var obj = new tournament { id_tournament = idFromFirstCell, id_kingdom = (int)curRowCells[1].Value, name = (string)curRowCells[2].Value, prize = (int)curRowCells[3].Value }; return(obj); } default: return(null); } }
public void iseethekidfighting(fight reaction) { seekidfight += reaction; }