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(); } } }
public LogUser GetLogUser(string userName) { LogUser user = null; using(TempDBLogUsers db = new TempDBLogUsers()) { user = db.LogUsers.Where(u => u.UserName == userName).FirstOrDefault(); } return user; }
public bool isInRole(string userName, string roleName) { bool isInRole = false; using(TempDBLogUsers db = new TempDBLogUsers()) { LogUser user = db.LogUsers.Where(u => u.UserName == userName).FirstOrDefault(); if(user != null) isInRole = user.Roles.Where(r => r.Name == roleName).Any(); } return isInRole; }
public IEnumerable<LogUser> GetAllLogUsers() { IEnumerable<LogUser> users = new List<LogUser>(); using(TempDBLogUsers db = new TempDBLogUsers()) { users = db.LogUsers.ToList(); } return users; }