/// <summary> /// Updates the Database with the state of a entity object /// </summary> /// <param name="entity">The entity to update</param> public void UpdateDB(EntityObject entity) //amazingly enough this is required to allow changing objects in the detached way my dal is working since im closing the context all the time { using (BlackJackDataEntities ctx = new BlackJackDataEntities()) { if (entity.EntityState == System.Data.EntityState.Modified) { object original = null; if (ctx.TryGetObjectByKey(entity.EntityKey, out original)) { ctx.ApplyCurrentValues(entity.EntityKey.EntitySetName, entity); } ctx.SaveChanges(); } } }
/// <summary> /// Remove a game from the DB /// </summary> /// <param name="game">Game to Remove</param> public void removeGame(Game game) { using (BlackJackDataEntities ctx = new BlackJackDataEntities()) { ctx.Game.Attach(game); ctx.DeleteObject(game); ctx.SaveChanges(); } }
/// <summary> /// Remove all games from DB used for Testing /// </summary> public void deleteAllGames() { using (BlackJackDataEntities ctx = new BlackJackDataEntities()) { foreach (Game g in GetGames()) { ctx.Game.DeleteObject(g); } ctx.SaveChanges(); } }
/// <summary> /// Remove all Users from DB used for testing /// </summary> public void deleteAllUsers() { using (BlackJackDataEntities ctx = new BlackJackDataEntities()) { ctx.SaveChanges(); foreach (User u in GetUsers()) { ctx.Attach(u); // to prevent from running the get all query again ctx.User.DeleteObject(u); } ctx.SaveChanges(); }; }
/// <summary> /// Add a user to the DB /// </summary> /// <param name="username">Username</param> /// <param name="password">Password</param> /// <param name="money" value="1000">Starting Money</param> public User AddUser(string username, string password, int money=1000,bool isAdmin=false) { using (BlackJackDataEntities ctx = new BlackJackDataEntities()) { var maxid = 1; if (ctx.User.Count() > 0) { maxid = ctx.User.Max(u => u.ID); maxid += 1; } User newuser = User.CreateUser(maxid,username,password,money,isAdmin); // not very cuncurrent and should not be used but the DB is limiting ctx.AddToUser(newuser); ctx.SaveChanges(); return newuser; } }
/// <summary> /// Add new Game Basic /// </summary> /// <param name="IP"></param> /// <param name="FirstUser"></param> public Game AddGame(string IP, string FirstUser) { using (BlackJackDataEntities ctx = new BlackJackDataEntities()) { var maxid = 1; if (ctx.Game.Count() > 0) { maxid = ctx.Game.Max(u => u.ID); maxid += 1; } Game newgame = Game.CreateGame(maxid,IP,FirstUser,1); // not very cuncurrent and should not be used but the DB is limiting ctx.AddToGame(newgame); ctx.SaveChanges(); return newgame; } }