Exemplo n.º 1
0
        public static IWebHost SeedDatabase(this IWebHost webHost, Action <SuperheroContext, IServiceProvider> seeder)
        {
            using (var scope = webHost.Services.CreateScope())
            {
                var configuration = scope.ServiceProvider.GetService <IConfiguration>();

                foreach (var connectionStringElement in configuration.GetSection("ConnectionStrings").GetChildren())
                {
                    var connectionString = connectionStringElement.Value;
                    var optionsBuilder   = new DbContextOptionsBuilder <SuperheroContext>();
                    optionsBuilder.UseSqlServer(connectionString);

                    using (var ctx = new SuperheroContext(optionsBuilder.Options))
                    {
                        seeder(ctx, scope.ServiceProvider);
                    }
                }
            }
            return(webHost);
        }
Exemplo n.º 2
0
 public TestController(SuperheroContext context)
 {
     _context = context;
 }