Esempio n. 1
0
        /// <summary>
        /// Method to see the database. Should not be used in production: demo purposes only.
        /// </summary>
        /// <param name="options">The configured options.</param>
        /// <param name="count">The number of contacts to seed.</param>
        /// <returns>The <see cref="Task"/>.</returns>
        private static async Task SeedDBAsync(DbContextOptions <JASContext> options, int count)
        {
            // empty to avoid logging while inserting (otherwise will flood console)
            var factory = new LoggerFactory();
            var builder = new DbContextOptionsBuilder <JASContext>(options)
                          .UseLoggerFactory(factory);

            using var context = new JASContext(builder.Options);
            // result is true if the database had to be created
            if (await context.Database.EnsureCreatedAsync())
            {
                var issueSeed = new SeedIssues();
                await issueSeed.SeedIssuesAsync(context);
            }

            RelationalDatabaseCreator databaseCreator =
                (RelationalDatabaseCreator)context.Database.GetService <IDatabaseCreator>();

            try
            {
                databaseCreator.CreateTables();
            }
            catch (Exception err) { }
            finally
            {
                Debug.WriteLine("Seeding Table");
                var issueSeed = new SeedIssues();
                await issueSeed.SeedIssuesAsync(context);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// This is NOT for production use. This extension method creates
 /// and populates the database for the first time to make the sample
 /// easiser to run. In production scenarios it could cause a race
 /// condition.
 /// </summary>
 /// <param name="factory">The <see cref="DbContextFactory{ContactContext}"/> to use.</param>
 /// <param name="count">The number of contacts to generate.</param>
 /// <returns>A <see cref="Task"/>.</returns>
 public static async Task EnsureDbCreatedAndSeedWithCountOfAsync(this DbContextOptions<JASContext> options, int count)
 {
   var builder = new DbContextOptionsBuilder<JASContext>(options);
   builder.UseLoggerFactory(new LoggerFactory());
   using var context = new JASContext(builder.Options);
   // result is true if the database had to be created
   if (await context.Database.EnsureCreatedAsync())
   {
     var seed = new SeedIssues();
     await seed.SeedIssuesAsync(context);
   }
 }