コード例 #1
0
        public static void SeedAdmin()
        {
            using (TorshiaDbContext context = new TorshiaDbContext())
            {
                string userRoleId  = GetRoleId("User");
                string adminRoleId = GetRoleId("Admin");

                var userRoles = new HashSet <UserRole>()
                {
                    new UserRole {
                        RoleId = userRoleId
                    },
                    new UserRole {
                        RoleId = adminRoleId
                    }
                };

                context.Users.Add(new User
                {
                    Email    = "*****@*****.**",
                    Password = HashPassword("admin"),
                    Username = "******",
                    Roles    = userRoles
                });

                context.SaveChanges();
            }
        }
コード例 #2
0
        private static void AddRoleIfNotExists(string roleName)
        {
            using (TorshiaDbContext context = new TorshiaDbContext())
            {
                if (!context.Roles.Any(r => r.Name == roleName))
                {
                    context.Roles.Add(new Role {
                        Name = roleName
                    });
                }

                context.SaveChanges();
            }
        }