public void DeleteUser(int userID) { using (DemoDBEntities1 db = new DemoDBEntities1()) { using (var dbContextTransaction = db.Database.BeginTransaction()) { try { var SUR = db.SYSUserRoles.Where(o => o.SYSUserID == userID); if (SUR.Any()) { db.SYSUserRoles.Remove(SUR.FirstOrDefault()); db.SaveChanges(); } var SUP = db.SYSUserProfiles.Where(o => o.SYSUserID == userID); if (SUP.Any()) { db.SYSUserProfiles.Remove(SUP.FirstOrDefault()); db.SaveChanges(); } var SU = db.SYSUsers.Where(o => o.SYSUserID == userID); if (SU.Any()) { db.SYSUsers.Remove(SU.FirstOrDefault()); db.SaveChanges(); } dbContextTransaction.Commit(); } catch { dbContextTransaction.Rollback(); } } } }
public void AddUserAccount(UserSignUpView user) { using (DemoDBEntities1 db = new DemoDBEntities1()) { SYSUser SU = new SYSUser(); SU.LoginName = user.LoginName; SU.PasswordEncryptedText = user.Password; SU.RowCreatedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1; SU.RowModifiedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1;; SU.RowCreatedDateTime = DateTime.Now; SU.RowMOdifiedDateTime = DateTime.Now; db.SYSUsers.Add(SU); db.SaveChanges(); SYSUserProfile SUP = new SYSUserProfile(); SUP.SYSUserID = SU.SYSUserID; SUP.FirstName = user.FirstName; SUP.LastName = user.LastName; SUP.Gender = user.Gender; SUP.RowCreatedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1; SUP.RowModifiedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1; SUP.RowCreatedDateTime = DateTime.Now; SUP.RowModifiedDateTime = DateTime.Now; db.SYSUserProfiles.Add(SUP); db.SaveChanges(); if (user.LOOKUPRoleID > 0) { SYSUserRole SUR = new SYSUserRole(); SUR.LOOKUPRoleID = user.LOOKUPRoleID; SUR.SYSUserID = user.SYSUserID; SUR.IsActive = true; SUR.RowCreatedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1; SUR.RowModifiedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1; SUR.RowCreatedDateTime = DateTime.Now; SUR.RowModifiedDateTime = DateTime.Now; db.SYSUserRoles.Add(SUR); db.SaveChanges(); } } }
public void UpdateUserAccount(UserProfileView user) { using (DemoDBEntities1 db = new DemoDBEntities1()) { using (var dbContextTransaction = db.Database.BeginTransaction()) { try { SYSUser SU = db.SYSUsers.Find(user.SYSUserID); SU.LoginName = user.LoginName; SU.PasswordEncryptedText = user.Password; SU.RowCreatedSYSUserID = user.SYSUserID; SU.RowModifiedSYSUserID = user.SYSUserID; SU.RowCreatedDateTime = DateTime.Now; SU.RowMOdifiedDateTime = DateTime.Now; db.SaveChanges(); var userProfile = db.SYSUserProfiles.Where(o => o.SYSUserID == user.SYSUserID); if (userProfile.Any()) { SYSUserProfile SUP = userProfile.FirstOrDefault(); SUP.SYSUserID = SU.SYSUserID; SUP.FirstName = user.FirstName; SUP.LastName = user.LastName; SUP.Gender = user.Gender; SUP.RowCreatedSYSUserID = user.SYSUserID; SUP.RowModifiedSYSUserID = user.SYSUserID; SUP.RowCreatedDateTime = DateTime.Now; SUP.RowModifiedDateTime = DateTime.Now; db.SaveChanges(); } if (user.LOOKUPRoleID > 0) { var userRole = db.SYSUserRoles.Where(o => o.SYSUserID == user.SYSUserID); SYSUserRole SUR = null; if (userRole.Any()) { SUR = userRole.FirstOrDefault(); SUR.LOOKUPRoleID = user.LOOKUPRoleID; SUR.SYSUserID = user.SYSUserID; SUR.IsActive = true; SUR.RowCreatedSYSUserID = user.SYSUserID; SUR.RowModifiedSYSUserID = user.SYSUserID; SUR.RowCreatedDateTime = DateTime.Now; SUR.RowModifiedDateTime = DateTime.Now; } else { SUR = new SYSUserRole(); SUR.LOOKUPRoleID = user.LOOKUPRoleID; SUR.SYSUserID = user.SYSUserID; SUR.IsActive = true; SUR.RowCreatedSYSUserID = user.SYSUserID; SUR.RowModifiedSYSUserID = user.SYSUserID; SUR.RowCreatedDateTime = DateTime.Now; SUR.RowModifiedDateTime = DateTime.Now; db.SYSUserRoles.Add(SUR); } db.SaveChanges(); } dbContextTransaction.Commit(); } catch { dbContextTransaction.Rollback(); } } } }