protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.UseEnvironment("Testing"); builder.ConfigureServices(services => { services.AddEntityFrameworkInMemoryDatabase(); var provider = services .AddEntityFrameworkInMemoryDatabase() .BuildServiceProvider(); services.AddDbContext <HireMeDbContext>(options => { options.UseInMemoryDatabase("InMemoryDbForTesting"); options.UseInternalServiceProvider(provider); }); var serviceProvider = services.BuildServiceProvider(); using (var scope = serviceProvider.CreateScope()) { var scopedServices = scope.ServiceProvider; Context = scopedServices.GetRequiredService <HireMeDbContext>(); Context.Database.EnsureCreated(); SeedData.Seed(Context, ConfigurationFactory.Create()); } }); }
public static HireMeDbContext Create() { var configuration = ConfigurationFactory.Create(); var options = new DbContextOptionsBuilder <HireMeDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var context = new HireMeDbContext(options); SeedData.Seed(context, configuration); return(context); }
public static void Seed(HireMeDbContext context, IConfiguration configuration) { var user = context.Users.SingleOrDefault(x => x.Username == configuration["Seed:DefaultUser:Username"]); if (user == null) { user = new User { Username = configuration["Seed:DefaultUser:Username"] }; user.Password = new PasswordHasher().HashPassword(user.Salt, configuration["Seed:DefaultUser:Password"]); user.Roles.Add(context.Roles.Single(x => x.Name == "Admin")); context.Users.Add(user); context.SaveChanges(); } }
public static void Seed(HireMeDbContext context) { var role = context.Roles.SingleOrDefault(x => x.Name == "Admin"); if (role == null) { role = new Role { Name = "Admin" }; context.Roles.Add(role); } var role1 = context.Roles.SingleOrDefault(x => x.Name == "Employeer"); if (role1 == null) { role1 = new Role { Name = "Employeer" }; context.Roles.Add(role1); } var role2 = context.Roles.SingleOrDefault(x => x.Name == "Candidate"); if (role2 == null) { role2 = new Role { Name = "Candidate" }; context.Roles.Add(role2); } context.SaveChanges(); }
public static void Seed(HireMeDbContext context, IConfiguration configuration) { RoleConfiguration.Seed(context); UserConfiguration.Seed(context, configuration); }