protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.ConfigureServices(services => { var descriptor = services.SingleOrDefault( d => d.ServiceType == typeof(DbContextOptions <ParkingLotContext>)); services.Remove(descriptor); services.AddDbContext <ParkingLotContext>(options => { InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(options, "InMemoryDbForTesting"); }); var sp = services.BuildServiceProvider(); using (var scope = sp.CreateScope()) { var scopedServices = scope.ServiceProvider; var db = scopedServices.GetRequiredService <ParkingLotContext>(); db.Database.EnsureCreated(); db.Database.EnsureDeleted(); } }); }
public static ChecklistBotContext CreateTestContext() { var builder = new DbContextOptionsBuilder <ChecklistBotContext>(); var b = InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(builder, "Data Source=:memory:"); return(new ChecklistBotContext(b.Options)); }
public CheckListServiceTest() { var builder = new DbContextOptionsBuilder <ChecklistBotContext>(); var b = InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(builder, "Data Source=:memory:"); _context = new ChecklistBotContext(b.Options); _service = new CheckListService(_context); }
public void CanInsertSamuraiWithSaveChanges() { // SQL Server var optionsBuilderSQLServer = new DbContextOptionsBuilder(); InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(optionsBuilderSQLServer, "SamuraiSQLData"); using (var context = new SQLServer.Data.SamuraiContext(optionsBuilderSQLServer.Options)) { var samurai = new Samurai { Name = "Julie" }; context.Samurais.Add(samurai); context.SaveChanges(); } using (var context = new SQLServer.Data.SamuraiContext(optionsBuilderSQLServer.Options)) { Assert.Equal(1, context.Samurais.Count()); } // Postgre SQL var optionsBuilderPostgreSQL = new DbContextOptionsBuilder(); InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(optionsBuilderPostgreSQL, "SamuraiPostgreSQL"); using (var context = new PostgreSQL.Data.SamuraiContext(optionsBuilderPostgreSQL.Options)) { var samurai = new Samurai { Name = "Julie" }; context.Samurais.Add(samurai); context.SaveChanges(); } using (var context = new PostgreSQL.Data.SamuraiContext(optionsBuilderPostgreSQL.Options)) { Assert.Equal(1, context.Samurais.Count()); } // SQLite var optionsBuilderSQLite = new DbContextOptionsBuilder(); InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(optionsBuilderSQLite, "SamuraiSQLite"); using (var context = new SQLite.Data.SamuraiContext(optionsBuilderSQLite.Options)) { var samurai = new Samurai { Name = "Julie" }; context.Samurais.Add(samurai); context.SaveChanges(); } using (var context = new SQLite.Data.SamuraiContext(optionsBuilderSQLite.Options)) { Assert.Equal(1, context.Samurais.Count()); } }
private CustomerContext GetInMemoryCustomerContext() { DbContextOptions <CustomerContext> options; var builder = new DbContextOptionsBuilder <CustomerContext>(); InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(builder, "InMemoryCustomerDb"); options = builder.Options; CustomerContext customerDataContext = new CustomerContext(options); customerDataContext.Database.EnsureDeleted(); customerDataContext.Database.EnsureCreated(); return(customerDataContext); }
public AuthIntegrationTests() { // App factory // Using memory virtual Database var authFactory = new WebApplicationFactory <Startup>() .WithWebHostBuilder(builder => { builder.ConfigureServices(services => { services.RemoveAll(typeof(UserContext)); services.AddDbContext <UserContext>(options => { InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(options, "RabbitaniaTestDB"); }); }); }); _client = authFactory.CreateClient(); }
/// <summary> /// This method will invoke the Breeze.EFPersistenceManager to extract the metadata for the /// </summary> /// <param name="dbContextType">The type to extract the csdl for</param> /// <returns>The metadata, or an empty string</returns> /// <remarks> /// </remarks> private static string GetMetadataFromType(Type dbContextType) { try { var providerType = typeof(EFPersistenceManager <>).MakeGenericType(dbContextType); var dbContextOptionsBuilderType = typeof(DbContextOptionsBuilder <>).MakeGenericType(dbContextType); var dbContextOptionsBuilder = (DbContextOptionsBuilder)Activator.CreateInstance(dbContextOptionsBuilderType); // by telling EF Core that we are using an in memory database we don't need to provide a connection string. 'foo' can be any name; it's not used. InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(dbContextOptionsBuilder, "foo", null); var dbContext = Activator.CreateInstance(dbContextType, new Object[] { dbContextOptionsBuilder.Options }); var provider = (PersistenceManager)Activator.CreateInstance(providerType, new Object[] { dbContext }); var metadata = provider.Metadata(); return(metadata); } catch (Exception ex) { Console.WriteLine("An exception was thrown while processing {0}. {1}", dbContextType.FullName, ex); } return(string.Empty); }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(optionsBuilder); }
/// <summary> /// Конструктор класса, где база инициализируется и заполняется тестовыми данными /// </summary> public ContextFixture() { var optionsBuilder = new DbContextOptionsBuilder <UserContext>(); var options = InMemoryDbContextOptionsExtensions.UseInMemoryDatabase <UserContext>(optionsBuilder, Guid.NewGuid().ToString()).Options; Context = new UserContext(options); var node1 = new Node() { Id = "node1", ParentId = null, TreeId = 1, Topic = "Node 1" }; var node2 = new Node() { Id = "node2", ParentId = null, TreeId = 2, Topic = "Node 2" }; var node3 = new Node() { Id = "node3", ParentId = null, TreeId = 3, Topic = "Node 3" }; Context.Nodes.Add(node1); Context.Nodes.Add(node2); Context.Nodes.Add(node3); var tree1 = new Tree() { Id = 1, Name = "Test tree 1", TreeType = TreeType.Production, UserId = 1 }; var tree2 = new Tree() { Id = 2, Name = "Test tree 2", TreeType = TreeType.Communications, UserId = 1 }; var tree3 = new Tree() { Id = 3, Name = "Test tree 3", TreeType = TreeType.ProjectBlock, UserId = 1 }; Context.Trees.Add(tree1); Context.Trees.Add(tree2); Context.Trees.Add(tree3); Context.SaveChanges(); }
private static void InitContext() { var builder = new DbContextOptionsBuilder <GameContext>(); InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(builder, "testDB"); var context = new GameContext(builder.Options); var questionsFacade = new QuestionsFacade(_gameContext); context.Database.EnsureCreated(); var team1 = new Team() { Name = "TopTestTeam", Score = 3000 }; var team2 = new Team() { Name = "Teletubbies", Score = 101 }; var team3 = new Team() { Name = "NotSoAnonymous", Score = 4999 }; var location1 = new Location() { Name = "Centraal Station", Street = "Koningin Astridplein 27,2018 Antwerpen,Belgium" }; var location2 = new Location() { Name = "David Teniers II", Street = "Teniersplaats 4, 2000 Antwerpen, Belgium" }; var location3 = new Location() { Name = "Paleis op de Meir", Street = "Meir 50, 2000 Antwerpen, Belgium" }; var Question1 = new Question() { Content = "In welk jaar is het dit station geopend?", Answer = "1905", Options = new string[] { "1908", "1901", "1905" }, Location = location1 }; var Question2 = new Question() { Content = "Hoe heet de schilder die op dit monument is afgebeeld?", Answer = "David Teniers II", Options = new string[] { "David Teniers II", "David Teniers I", "Robert Campin" }, Location = location2 }; var Question3 = new Question() { Content = "In welke eeuw is deze schilder geboren?", Answer = "17e", Options = new string[] { "18e", "17e", "16e" }, Location = location2 }; var Question4 = new Question() { Content = "In welk jaar is dit paleis gebouwd?", Answer = "1745", Options = new string[] { "1690", "1710", "1745" }, Location = location3 }; var Question5 = new Question() { Content = "Welke koning liet er de Spiegelzaal aanleggen?", Answer = "Leopold II", Options = new string[] { "Leopold II", "Napoleon Bonaparte", "Willem I" }, Location = location3 }; context.Locations.Add(location1); context.Locations.Add(location2); context.Locations.Add(location3); context.Questions.Add(Question1); context.Questions.Add(Question2); context.Questions.Add(Question3); context.Questions.Add(Question4); context.Questions.Add(Question5); context.Teams.Add(team1); context.Teams.Add(team2); context.Teams.Add(team3); context.SaveChanges(); _gameContext = context; }