public DropCreateAndSeedDatabaseAlways(IDataContextSeeder <TContext> seeder) { if (seeder == null) { throw new ArgumentNullException("seeder"); } _seeder = seeder; }
public ConfigurationWithSeeder(TDataSeeder seeder) { if (seeder == null) { throw new ArgumentNullException("seeder"); } _seeder = seeder; AutomaticMigrationsEnabled = false; ContextKey = typeof(TContext).Name; }
protected virtual void SeedDatabase(TestServer server, TestDatabaseConfiguration databaseConfiguration, DbContext dbContext) { // Resolve a context seeder and seed the database with the individual seeders IDataContextSeeder contextSeeder = server.Host.Services.GetRequiredService <IDataContextSeeder>(); IEntitySeeder[] entitySeeders = databaseConfiguration.SeedBuilder.GetEntitySeeders(); contextSeeder.SeedDataContext(dbContext, entitySeeders); // Always run the callback seeder last so that individual tests have the ability to replace data // inserted via seeders. databaseConfiguration.DatabaseSeeder?.DynamicInvoke(dbContext); }
private static async Task Seed(IDataContextSeeder <IdentityDataContext> seeder, IServiceScope scope) { var assembly = typeof(IdentityDataContext).Assembly; var hosting = scope.ServiceProvider.GetRequiredService <IHostingEnvironment>(); var environment = hosting.EnvironmentName; await seeder.SeedAsync <ApiResource, IdentityServer4.EntityFramework.Entities.ApiResource>( assembly, $"NativeCode.Node.Identity.Seeding.{environment}.ApiResource.json", (model, dbset) => dbset.SingleOrDefaultAsync(x => x.Name == model.Name), (model, dbset) => Task.FromResult(model.ToEntity())); await seeder.SeedAsync <Client, IdentityServer4.EntityFramework.Entities.Client>( assembly, $"NativeCode.Node.Identity.Seeding.{environment}.Client.json", (model, dbset) => dbset.SingleOrDefaultAsync(x => x.ClientId == model.ClientId), (model, dbset) => Task.FromResult(model.ToEntity())); await seeder.SeedAsync <IdentityResource, IdentityServer4.EntityFramework.Entities.IdentityResource>( assembly, "NativeCode.Node.Identity.Seeding.IdentityResource.json", (model, dbset) => dbset.SingleOrDefaultAsync(x => x.Name == model.Name), (model, dbset) => Task.FromResult(model.ToEntity())); await seeder.SaveChangesAsync(); await seeder.SeedAsync <UserInfo, User>( assembly, $"NativeCode.Node.Identity.Seeding.{environment}.User.json", (model, dbset) => dbset.SingleOrDefaultAsync(x => x.Email == model.Email), async (model, dbset) => { using (var inner = scope.ServiceProvider.CreateScope()) using (var users = inner.ServiceProvider.GetRequiredService <UserManager <User> >()) { var user = new User { Email = model.Email, EmailConfirmed = model.EmailConfirmed, UserName = model.UserName, }; var result = await users.CreateAsync(user, model.Password); if (result.Succeeded == false) { throw new AggregateException(result.Errors.Select(x => new InvalidOperationException(x.Description))); } return(await dbset.FindAsync(user.Id)); } }); }
public DataContextSeederBehavior( IDataContextAccessor decoratedDataContextProducer, IDataContextSeeder dataContextSeeder) { _decoratee = decoratedDataContextProducer ?? throw new ArgumentNullException( nameof(decoratedDataContextProducer), ErrorMessageResources.ArgumentExpected.StringFormat( nameof(DataContextSeederBehavior), nameof(decoratedDataContextProducer))); _seeder = dataContextSeeder ?? throw new ArgumentNullException( nameof(dataContextSeeder), ErrorMessageResources.ArgumentExpected.StringFormat( nameof(DataContextSeederBehavior), nameof(dataContextSeeder))); }
public ConfigurationWithSeeder() { _seeder = new TDataSeeder(); AutomaticMigrationsEnabled = false; ContextKey = typeof(TContext).Name; }