Exemplo n.º 1
0
        protected override void Seed(Infrastructure.DbContext.MyIdentityDbContext context)
        {
            MyUserManager userManager = new MyUserManager(new UserStore <MyUser>(context));
            MyRoleManager roleManager = new MyRoleManager(new RoleStore <MyRole>(context));

            string roleName = "Administrator";
            string userName = "******";
            string password = "******";
            string email    = "*****@*****.**";

            if (!roleManager.RoleExists(roleName))
            {
                roleManager.Create(new MyRole(roleName));
            }

            MyUser user = userManager.FindByName(userName);

            if (user == null)
            {
                userManager.Create(new MyUser {
                    UserName = userName, Email = email
                },
                                   password);
                user = userManager.FindByName(userName);
            }

            if (!userManager.IsInRole(user.Id, roleName))
            {
                userManager.AddToRole(user.Id, roleName);
            }
            foreach (MyUser dbUser in userManager.Users)
            {
                if (dbUser.Country == Countries.None)
                {
                    dbUser.SetCountryFromCity(dbUser.City);
                }
            }
            context.SaveChanges();
        }