/// <summary> /// Initializes a new instance of the <see cref="Insert"/> class. /// Seeds the database. /// </summary> public Insert() { // Create a service provider to be shared by all test methods var serviceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider(); // Create options telling the context to use an // InMemory database and the service provider. var builder = new DbContextOptionsBuilder <TemplateDbContext>(); builder.UseInMemoryDatabase().UseInternalServiceProvider(serviceProvider); this.contextOptions = builder.Options; // seed in constructor. using (var context = new TemplateDbContext(this.contextOptions)) { var roleStore = new RoleStore <IdentityRole>(context); var role = new IdentityRole { Name = "User", NormalizedName = "User" }; roleStore.CreateAsync(role); context.SaveChangesAsync(); this.roleId = role.Id; } }
/// <summary> /// Initializes a new instance of the <see cref="FindBy"/> class. /// </summary> public GetAll() { // Create a service provider to be shared by all test methods var serviceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider(); // Create options telling the context to use an // InMemory database and the service provider. var builder = new DbContextOptionsBuilder <TemplateDbContext>(); builder.UseInMemoryDatabase().UseInternalServiceProvider(serviceProvider); this.contextOptions = builder.Options; // seed in constructor. using (var context = new TemplateDbContext(this.contextOptions)) { string password = "******"; var roleStore = new RoleStore <IdentityRole>(context); var user = new ApplicationUser { Name = "User for test purposes", UserName = "******", NormalizedUserName = "******", Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", EmailConfirmed = true, LockoutEnabled = false, SecurityStamp = Guid.NewGuid().ToString() }; var userTester = new ApplicationUser { Name = "User for test purposes", UserName = "******", NormalizedUserName = "******", Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", EmailConfirmed = true, LockoutEnabled = false, SecurityStamp = Guid.NewGuid().ToString() }; if (!context.Roles.Any(r => r.Name == "User")) { roleStore.CreateAsync(new IdentityRole { Name = "User", NormalizedName = "User" }); } if (!context.Users.Any(u => u.UserName == user.UserName)) { var hasher = new PasswordHasher <ApplicationUser>(); var hashed = hasher.HashPassword(user, password); user.PasswordHash = hashed; userTester.PasswordHash = hashed; var userStore = new UserStore <ApplicationUser>(context); userStore.CreateAsync(user); userStore.CreateAsync(userTester); userStore.AddToRoleAsync(user, "User"); userStore.AddToRoleAsync(userTester, "User"); } context.SaveChangesAsync(); } }
/// <summary> /// Initializes a new instance of the <see cref="UpdateUserRoles"/> class. /// Seeds the database. /// </summary> public UpdateUserRoles() { // Create a service provider to be shared by all test methods var serviceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider(); // Create options telling the context to use an // InMemory database and the service provider. var builder = new DbContextOptionsBuilder <TemplateDbContext>(); builder.UseInMemoryDatabase().UseInternalServiceProvider(serviceProvider); this.contextOptions = builder.Options; // seed in constructor. using (var context = new TemplateDbContext(this.contextOptions)) { string password = "******"; var roleStore = new RoleStore <IdentityRole>(context); var user = new ApplicationUser { Name = "User for test purposes", UserName = "******", NormalizedUserName = "******", Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", EmailConfirmed = true, LockoutEnabled = false, SecurityStamp = Guid.NewGuid().ToString() }; var role = new IdentityRole { Name = "User", NormalizedName = "User" }; roleStore.CreateAsync(role); var administratorRole = new IdentityRole { Name = "Administrator", NormalizedName = "Administrator" }; roleStore.CreateAsync(administratorRole); var executeReportsRole = new IdentityRole { Name = "Execute Reports Role", NormalizedName = "Execute Reports Role " }; roleStore.CreateAsync(executeReportsRole); if (!context.Users.Any(u => u.UserName == user.UserName)) { var hasher = new PasswordHasher <ApplicationUser>(); var hashed = hasher.HashPassword(user, password); user.PasswordHash = hashed; var userStore = new UserStore <ApplicationUser>(context); userStore.CreateAsync(user); } context.SaveChangesAsync(); context.UserRoles.Add(new IdentityUserRole <string> { RoleId = role.Id, UserId = user.Id }); context.SaveChanges(); // after save changes, can use references of Ids. this.userId = user.Id; this.userRoleId = role.Id; this.administratorRoleId = administratorRole.Id; this.reporterRoleId = executeReportsRole.Id; } }
public async Task <int> SaveChanges() { return(await Db.SaveChangesAsync()); }