コード例 #1
0
ファイル: Init.cs プロジェクト: sasha007k/PhoneStore
        public static async Task SeedUsers(UserManager <UserModel> userManager, PhoneStoreDbContext context)
        {
            string username = "******";
            string password = "******";

            if (await userManager.FindByNameAsync(username) == null)
            {
                UserModel admin = new UserModel()
                {
                    UserName = username, Email = username
                };
                IdentityResult result = await userManager.CreateAsync(admin, password);

                if (result.Succeeded)
                {
                    var shoppingCart = new ShoppingCart()
                    {
                        User = admin, UserId = admin.Id
                    };
                    admin.ShoppingCart = shoppingCart;
                    await context.ShoppingCarts.AddAsync(shoppingCart);

                    await context.SaveChangesAsync();

                    await userManager.AddToRoleAsync(admin, "Admin");

                    await userManager.AddToRoleAsync(admin, "Shopper");
                }
            }
        }
コード例 #2
0
ファイル: Init.cs プロジェクト: sasha007k/PhoneStore
 public static async Task SeedData(UserManager <UserModel> userManager, RoleManager <IdentityRole> roleManager, PhoneStoreDbContext context)
 {
     await SeedRoles(roleManager);
     await SeedUsers(userManager, context);
 }