public void CreateUser(Models.User user) { using(TempDBContext1 db = new TempDBContext1()) { db.Users.Add(user); db.SaveChanges(); } }
public void UpdateUser(Models.User user) { using (TempDBContext1 db = new TempDBContext1()) { User oldUser = db.Users.Find(user.UserName); oldUser.CurrentHealth = user.CurrentHealth; oldUser.MonstersKilled = user.MonstersKilled; oldUser.RoomsTraveled = user.RoomsTraveled; oldUser.Attack = user.Attack; oldUser.Defense = user.Defense; oldUser.AttackSpeed = user.AttackSpeed; oldUser.AttackRange = user.AttackRange; oldUser.MoveSpeed = user.MoveSpeed; db.SaveChanges(); } }
public void DeleteUser(string userName) { //Remove user from Friend table using(SqlDBFriendListContext db = new SqlDBFriendListContext()) { List<Friend> friends = db.Friends.Where(u => (u.Friend1 == userName) || (u.User == userName)).ToList(); foreach(Friend friend in friends) { db.Friends.Remove(friend); db.Entry(friend).State = EntityState.Deleted; } db.SaveChanges(); } //Remove user from FriendRequest and Message table using(TempDBFriendContext db = new TempDBFriendContext()) { List<FriendRequest> friends = db.FriendRequests.Where(u => (u.Sender == userName) || (u.Receiver == userName)).ToList(); List<Message> messages = db.Messages.Where(u => (u.Sender == userName) || (u.Receiver == userName)).ToList(); foreach (FriendRequest friend in friends) { db.FriendRequests.Remove(friend); db.Entry(friend).State = EntityState.Deleted; } foreach(Message m in messages) { db.Messages.Remove(m); db.Entry(m).State = EntityState.Deleted; } db.SaveChanges(); } //Remove user from User table using(TempDBContext1 db = new TempDBContext1()) { User user = db.Users.Where(u => u.UserName == userName).FirstOrDefault(); if (user != null) { //db.Users.Attach(user); db.Users.Remove(user); db.Entry(user).State = EntityState.Deleted; db.SaveChanges(); } } //Remove user from LogUser table and UserRoles table using(TempDBLogUsers db = new TempDBLogUsers()) { LogUser user = db.LogUsers.Where(u => u.UserName == userName).FirstOrDefault(); if (user != null) { user.Roles.FirstOrDefault().LogUsers.Remove(user); user.Roles.Remove(user.Roles.FirstOrDefault()); //db.LogUsers.Attach(user); db.LogUsers.Remove(user); db.Entry(user).State = EntityState.Deleted; db.SaveChanges(); } } }