Home > Enterprise >  Seeding - Adding user roles
Seeding - Adding user roles

Time:03-28

i recently picked c# and i am stuck at adding user roles and admin. While i have been able to add the roles(seedroles, i cant seem to add admin(seeduser).

When i hover on on the "administrator" paremeter i try to add as a role fro the administrator it says "cannot convert from string to systems.collections.generic.IEnumerable<string>

using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace leave_management
{
    public static class SeedData
    {
        public static void Seed(UserManager<IdentityUser> userManager, RoleManager<IdentityRole> roleManager)
        {
            SeedRoles(roleManager);
            SeedUsers(userManager);
        }

        private static void SeedUsers(UserManager<IdentityUser> userManager)
         {
            if (userManager.FindByNameAsync("admin").Result == null)
            {

                var user = new IdentityUser
                {
                    UserName = "[email protected]",
                    Email = "[email protected]",

                };

                var result = userManager.CreateAsync(user, "@password1").Result;
                if (result.Succeeded)
                {
                    //THis is where the error is at
                    userManager.AddToRolesAsync(user, "Administrator").Wait();
                }
            }
        }

        private static void SeedRoles(RoleManager<IdentityRole> roleManager)
        {
            if (!roleManager.RoleExistsAsync("Administrator").Result)
            {
                var role = new IdentityRole
                {
                    Name = "Administrator"
                };
                var result = roleManager.CreateAsync(role).Result;
            }

            if (!roleManager.RoleExistsAsync("Employee").Result)
            {
                var role = new IdentityRole
                {
                    Name = "Employee"
                };
                var result = roleManager.CreateAsync(role).Result;
            }
        }
    }
}

When i hover on on the "administrator" paremeter i try to add as a role fro the administrator it says "cannot convert from string to systems.collections.generic.IEnumerable<string>

CodePudding user response:

You're trying to pass a string to something that wants an enumerable of strings. You can't do that. Put the string into a collection if you need to.

new string[] {administrator};
  • Related