public static Game GetGameByKey(int idGame) { Game oGame = new Game(); try { using (var ctx = new PollaExpressDBEntities()) { ctx.Configuration.ProxyCreationEnabled = false; oGame = ctx.Game.Where(x => x.id.Equals(idGame)).FirstOrDefault(); } } catch (Exception ex) { throw ex; } return oGame; }
public static void SaveGame(Game game) { try { using (var ctx = new PollaExpressDBEntities()) { //verify if the student exists Game oGame = GetGameByKey(game.id); if (oGame != null) { // if exists then edit ctx.Game.Attach(oGame); EntityFrameworkHelper.EnumeratePropertyDifferences(oGame, game); ctx.SaveChanges(); } else { // else create ctx.Game.Add(game); ctx.SaveChanges(); } } } catch (DbEntityValidationException e) { StringBuilder oError = new StringBuilder(); foreach (var eve in e.EntityValidationErrors) { oError.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State)); foreach (var ve in eve.ValidationErrors) { oError.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage)); } } string msg = oError.ToString(); throw new Exception(msg); } catch (Exception ex) { throw ex; } }
public void SaveGame(Game game) { GameCRUD.SaveGame(game); }