Skip to content

Instantly share code, notes, and snippets.

@gmartinezsan
Last active April 19, 2018 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmartinezsan/5ace678988c7290adb69ea9ed7b107e2 to your computer and use it in GitHub Desktop.
Save gmartinezsan/5ace678988c7290adb69ea9ed7b107e2 to your computer and use it in GitHub Desktop.
using System;
using BooksWebApi.Data;
using BooksWebApi.Entities;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace BooksWebApi
{
public class Program
{
public static void Main(string[] args)
{
var host = BuildWebHost(args);
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<BooksCatalogDbContext>();
var userManager = services.GetRequiredService<UserManager<User>>();
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
DataInitializer.Seed(userManager, roleManager, context);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while seeding the database.");
}
}
host.Run();
}
public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment