private static void SaveDb(string actionName, int userId, string ip) { using (var db = new AppDbContext()) { var activity = new UserActivity { User = db.Users.Find(userId), Time = DateTime.UtcNow, IpAddress = ip, Action = actionName }; db.UserActivities.Add(activity); db.SaveChanges(); } }
private static void AddUserRoles(AppDbContext dbContext) { var roles = new List<UserRole> { new UserRole {Name = "Administrator", Description = "系统管理员,负责用户管理,角色分配,数据备份"}, new UserRole {Name = "Reporter", Description = "报修员,由各班班干部或班主任负责"}, new UserRole {Name = "MaintenanceStaff", Description = "维修人员,由学校总务处派出"}, new UserRole {Name = "Supervisor", Description = "监督者,学校总务处主管领导"} }; roles.ForEach(r => dbContext.UserRoles.Add(r)); var initialUser = new User { UserName = "******", DisplayName = "Initial User", Grade = -1, IsEnabled = true, Roles = new HashSet<UserRole>(), PasswordHash = new PasswordHasher().HashPassword("admin") }; initialUser.Roles.Add(roles[0]); dbContext.Users.Add(initialUser); dbContext.SaveChanges(); }