public static async Task EnsureSeedData(IServiceScope scope, IConfiguration configuration, Extensions.Logging.ILogger logger)
    {
        var retryPolicy = CreateRetryPolicy(configuration, logger);
        var context     = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();

        await retryPolicy.ExecuteAsync(async() =>
        {
            await context.Database.MigrateAsync();

            var userMgr = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
            var alice   = await userMgr.FindByNameAsync("alice");

            if (alice == null)
            {
                alice = new ApplicationUser
                {
                    UserName       = "******",
                    Email          = "*****@*****.**",
                    EmailConfirmed = true,
                    CardHolderName = "Alice Smith",
                    CardNumber     = "4012888888881881",
                    CardType       = 1,
                    City           = "Redmond",
                    Country        = "U.S.",
                    Expiration     = "12/20",
                    Id             = Guid.NewGuid().ToString(),
                    LastName       = "Smith",
                    Name           = "Alice",
                    PhoneNumber    = "1234567890",
                    ZipCode        = "98052",
                    State          = "WA",
                    Street         = "15703 NE 61st Ct",
                    SecurityNumber = "123"
                };

                var result = userMgr.CreateAsync(alice, "Pass123$").Result;

                if (!result.Succeeded)
                {
                    throw new Exception(result.Errors.First().Description);
                }

                logger.LogDebug("alice created");
            }
            else
            {
                logger.LogDebug("alice already exists");
            }

            var bob = await userMgr.FindByNameAsync("bob");

            if (bob == null)
            {
                bob = new ApplicationUser
                {
                    UserName       = "******",
                    Email          = "*****@*****.**",
                    EmailConfirmed = true,
                    CardHolderName = "Bob Smith",
                    CardNumber     = "4012888888881881",
                    CardType       = 1,
                    City           = "Redmond",
                    Country        = "U.S.",
                    Expiration     = "12/20",
                    Id             = Guid.NewGuid().ToString(),
                    LastName       = "Smith",
                    Name           = "Bob",
                    PhoneNumber    = "1234567890",
                    ZipCode        = "98052",
                    State          = "WA",
                    Street         = "15703 NE 61st Ct",
                    SecurityNumber = "456"
                };

                var result = await userMgr.CreateAsync(bob, "Pass123$");

                if (!result.Succeeded)
                {
                    throw new Exception(result.Errors.First().Description);
                }

                logger.LogDebug("bob created");
            }
            else
            {
                logger.LogDebug("bob already exists");
            }
        });
    }
Esempio n. 2
0
 public void LogDebug(string message) => _logger.LogDebug(message);