public async Task Can_use_one_context_nested_inside_another_of_the_same_type() { using (OracleTestStore.GetNorthwindStore()) { var serviceProvider = new ServiceCollection() .AddEntityFrameworkOracle() .BuildServiceProvider(); using (var context1 = new NorthwindContext(serviceProvider)) { var customers1 = await context1.Customers.ToListAsync(); Assert.Equal(91, customers1.Count); Assert.Equal(91, context1.ChangeTracker.Entries().Count()); using (var context2 = new NorthwindContext(serviceProvider)) { Assert.Equal(0, context2.ChangeTracker.Entries().Count()); var customers2 = await context2.Customers.ToListAsync(); Assert.Equal(91, customers2.Count); Assert.Equal(91, context2.ChangeTracker.Entries().Count()); Assert.Equal(customers1[0].CustomerID, customers2[0].CustomerID); Assert.NotSame(customers1[0], customers2[0]); } } } }
private static async Task Noop_when_database_does_not_exist_test(bool async) { using (var testDatabase = OracleTestStore.Create("NonExisting")) { using (var context = new OracleDatabaseCreatorTest.BloggingContext(testDatabase)) { var creator = OracleDatabaseCreatorTest.GetDatabaseCreator(context); Assert.False(async ? await creator.ExistsAsync() : creator.Exists()); if (async) { Assert.False(await creator.EnsureDeletedAsync()); } else { Assert.False(creator.EnsureDeleted()); } Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State); Assert.False(async ? await creator.ExistsAsync() : creator.Exists()); Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State); } } }
public void Insert_with_client_generated_GUID_key() { using (var testStore = OracleTestStore.CreateInitialized(DatabaseName)) { Guid afterSave; using (var context = new BlogContextClientGuidKey(testStore.Name)) { context.Database.EnsureCreated(); var blog = context.Add(new GuidBlog { Name = "One Unicorn" }).Entity; var beforeSave = blog.Id; var beforeSaveNotId = blog.NotId; Assert.NotEqual(default(Guid), beforeSave); Assert.NotEqual(default(Guid), beforeSaveNotId); context.SaveChanges(); afterSave = blog.Id; var afterSaveNotId = blog.NotId; Assert.Equal(beforeSave, afterSave); Assert.Equal(beforeSaveNotId, afterSaveNotId); } using (var context = new BlogContextClientGuidKey(testStore.Name)) { Assert.Equal(afterSave, context.GuidBlogs.Single().Id); } } }
private static async Task Creates_physical_database_but_not_tables_test(bool async) { using (var testDatabase = OracleTestStore.GetOrCreate("CreateTest")) { var creator = OracleDatabaseCreatorTest.GetDatabaseCreator(testDatabase); creator.EnsureDeleted(); if (async) { await creator.CreateAsync(); } else { creator.Create(); } Assert.True(creator.Exists()); if (testDatabase.ConnectionState != ConnectionState.Open) { await testDatabase.OpenConnectionAsync(); } Assert.Equal( 0, (await testDatabase.QueryAsync <string>( "SELECT table_name FROM user_tables")).Count()); } }
public void Insert_with_key_default_value_from_sequence() { using (var testStore = OracleTestStore.CreateInitialized(DatabaseName)) { using (var context = new BlogContextKeyColumnWithDefaultValue(testStore.Name)) { context.Database.EnsureCreated(); context.AddRange(new Blog { Name = "One Unicorn" }, new Blog { Name = "Two Unicorns" }); context.SaveChanges(); } using (var context = new BlogContextKeyColumnWithDefaultValue(testStore.Name)) { var blogs = context.Blogs.OrderBy(e => e.Id).ToList(); Assert.Equal(77, blogs[0].Id); Assert.Equal(78, blogs[1].Id); } } }
public void Update_explicit_value_in_computed_column() { using (var testStore = OracleTestStore.CreateInitialized(DatabaseName)) { using (var context = new BlogContextComputedColumn(testStore.Name)) { context.Database.EnsureCreated(); context.Add(new FullNameBlog { FirstName = "One", LastName = "Unicorn" }); context.SaveChanges(); } using (var context = new BlogContextComputedColumn(testStore.Name)) { var blog = context.FullNameBlogs.Single(); blog.FullName = "The Gorilla"; // The property 'FullName' on entity type 'FullNameBlog' is defined to be read-only after it has been saved, // but its value has been modified or marked as modified. Assert.Equal( CoreStrings.PropertyReadOnlyAfterSave("FullName", "FullNameBlog"), Assert.Throws <InvalidOperationException>(() => context.SaveChanges()).Message); } } }
public void Insert_with_explicit_with_default_keys() { using (var testStore = OracleTestStore.CreateInitialized(DatabaseName)) { using (var context = new BlogContextNoKeyGenerationNullableKey(testStore.Name)) { context.Database.EnsureCreated(); context.AddRange( new NullableKeyBlog { Id = 0, Name = "One Unicorn" }, new NullableKeyBlog { Id = 1, Name = "Two Unicorns" }); context.SaveChanges(); } using (var context = new BlogContextNoKeyGenerationNullableKey(testStore.Name)) { var blogs = context.NullableKeyBlogs.OrderBy(e => e.Id).ToList(); Assert.Equal(0, blogs[0].Id); Assert.Equal(1, blogs[1].Id); } } }
public void Can_use_AddDbContext_and_get_connection_string_from_config(string key, string connectionString) { var configBuilder = new ConfigurationBuilder() .AddInMemoryCollection( new Dictionary <string, string> { { key, OracleNorthwindTestStoreFactory.NorthwindConnectionString } }); var serviceProvider = new ServiceCollection() .AddSingleton <IConfiguration>(configBuilder.Build()) .AddDbContext <UseConfigurationContext>( b => b.UseOracle(connectionString)) .BuildServiceProvider(); using (OracleTestStore.GetNorthwindStore()) { using (var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope()) { using (var context = serviceScope.ServiceProvider.GetRequiredService <UseConfigurationContext>()) { Assert.True(context.Customers.Any()); } } } }
public void Insert_and_update_with_computed_column() { using (var testStore = OracleTestStore.CreateInitialized(DatabaseName)) { using (var context = new BlogContextComputedColumn(testStore.Name)) { context.Database.EnsureCreated(); var blog = context.Add(new FullNameBlog { FirstName = "One", LastName = "Unicorn" }).Entity; context.SaveChanges(); Assert.Equal("One Unicorn", blog.FullName); } using (var context = new BlogContextComputedColumn(testStore.Name)) { var blog = context.FullNameBlogs.Single(); Assert.Equal("One Unicorn", blog.FullName); blog.LastName = "Pegasus"; context.SaveChanges(); Assert.Equal("One Pegasus", blog.FullName); } } }
public async Task Insert_batch_record_async() { using (var testDatabase = OracleTestStore.CreateInitialized(DatabaseName)) { var options = Fixture.CreateOptions(testDatabase); using (var context = new OracleBatchInsertContext(options)) { await context.GetService <IRelationalDatabaseCreator>().CreateTablesAsync(); for (int i = 0; i < 5000; i++) { context.Add(new Movie { Description = $"The EntityFramework {i}", Publication = DateTime.Now }); } var rows = await context.SaveChangesAsync(); Assert.Equal(1, (await context.Movies.SingleAsync(e => e.Description == "The EntityFramework 0")).Id); Assert.Equal(5000, (await context.Movies.LastAsync()).Id); Assert.Equal(5000, rows); Assert.NotEqual(4999, rows); } } }
public void Insert_with_sequence_HiLo() { using (var testStore = OracleTestStore.CreateInitialized(DatabaseName)) { using (var context = new BlogContextHiLo(testStore.Name)) { context.Database.EnsureCreated(); context.AddRange(new Blog { Name = "One Unicorn" }, new Blog { Name = "Two Unicorns" }); context.SaveChanges(); } using (var context = new BlogContextHiLo(testStore.Name)) { var blogs = context.Blogs.OrderBy(e => e.Id).ToList(); Assert.Equal(1, blogs[0].Id); Assert.Equal(2, blogs[0].OtherId); Assert.Equal(3, blogs[1].Id); Assert.Equal(4, blogs[1].OtherId); } } }
private static async Task Delete_database_test(bool async, bool open) { using (var testDatabase = OracleTestStore.CreateInitialized("EnsureDeleteBlogging")) { if (!open) { testDatabase.CloseConnection(); } using (var context = new OracleDatabaseCreatorTest.BloggingContext(testDatabase)) { var creator = OracleDatabaseCreatorTest.GetDatabaseCreator(context); Assert.True(async ? await creator.ExistsAsync() : creator.Exists()); if (async) { Assert.True(await context.Database.EnsureDeletedAsync()); } else { Assert.True(context.Database.EnsureDeleted()); } Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State); Assert.False(async ? await creator.ExistsAsync() : creator.Exists()); Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State); } } }
public void Can_track_an_entity_with_more_than_10_properties() { using (var testDatabase = OracleTestStore.CreateInitialized(DatabaseName)) { var options = Fixture.CreateOptions(testDatabase); using (var context = new GameDbContext(options)) { context.Database.EnsureCreated(); context.Characters.Add(new PlayerCharacter(new Level { Game = new Game() })); context.SaveChanges(); } using (var context = new GameDbContext(options)) { var character = context.Characters .Include(c => c.Level.Game) .OrderBy(c => c.Id) .First(); Assert.NotNull(character.Game); Assert.NotNull(character.Level); Assert.NotNull(character.Level.Game); } } }
private static async Task Returns_false_when_database_exists_but_has_no_tables_test(bool async) { using (var testDatabase = OracleTestStore.GetOrCreateInitialized("Empty")) { var creator = OracleDatabaseCreatorTest.GetDatabaseCreator(testDatabase); Assert.False(async ? await creator.HasTablesAsyncBase() : creator.HasTablesBase()); } }
private static async Task Can_use_an_existing_closed_connection_test(bool openConnection) { var serviceProvider = new ServiceCollection() .AddEntityFrameworkOracle() .BuildServiceProvider(); using (var store = OracleTestStore.GetNorthwindStore()) { store.CloseConnection(); var openCount = 0; var closeCount = 0; var disposeCount = 0; using (var connection = new OracleConnection(store.ConnectionString)) { if (openConnection) { await connection.OpenAsync(); } connection.StateChange += (_, a) => { switch (a.CurrentState) { case ConnectionState.Open: openCount++; break; case ConnectionState.Closed: closeCount++; break; } }; connection.Disposed += (_, __) => disposeCount++; using (var context = new NorthwindContext(serviceProvider, connection)) { Assert.Equal(91, await context.Customers.CountAsync()); } if (openConnection) { Assert.Equal(ConnectionState.Open, connection.State); Assert.Equal(0, openCount); Assert.Equal(0, closeCount); } else { Assert.Equal(ConnectionState.Closed, connection.State); Assert.Equal(1, openCount); Assert.Equal(1, closeCount); } Assert.Equal(0, disposeCount); } } }
public async Task Can_save_changes_in_tracked_entities() { using (var testDatabase = OracleTestStore.CreateInitialized(DatabaseName)) { int updatedId; int deletedId; int addedId; var options = Fixture.CreateOptions(testDatabase); using (var db = new BloggingContext(options)) { var blogs = await CreateBlogDatabaseAsync <Blog>(db); var toAdd = db.Blogs.Add( new Blog { Name = "Blog to Insert", George = true, TheGu = new Guid("0456AEF1-B7FC-47AA-8102-975D6BA3A9BF"), NotFigTime = new DateTime(1973, 9, 3, 0, 10, 33, 777), ToEat = 64, OrNothing = 0.123456789, Fuse = 777, WayRound = 9876543210, Away = 0.12345f, AndChew = new byte[16] }).Entity; db.Entry(toAdd).State = EntityState.Detached; var toUpdate = blogs[0]; toUpdate.Name = "Blog is Updated"; updatedId = toUpdate.Id; var toDelete = blogs[1]; toDelete.Name = "Blog to delete"; deletedId = toDelete.Id; db.Remove(toDelete); db.Entry(toAdd).State = EntityState.Added; await db.SaveChangesAsync(); addedId = toAdd.Id; Assert.NotEqual(0, addedId); Assert.Equal(EntityState.Unchanged, db.Entry(toUpdate).State); Assert.Equal(EntityState.Unchanged, db.Entry(toAdd).State); Assert.DoesNotContain(toDelete, db.ChangeTracker.Entries().Select(e => e.Entity)); } using (var db = new BloggingContext(options)) { var toUpdate = db.Blogs.Single(b => b.Id == updatedId); Assert.Equal("Blog is Updated", toUpdate.Name); Assert.Equal(0, db.Blogs.Count(b => b.Id == deletedId)); Assert.Equal("Blog to Insert", db.Blogs.Single(b => b.Id == addedId).Name); } } }
private static async Task Creates_schema_in_existing_database_test(bool async) { using (var testDatabase = OracleTestStore.GetOrCreateInitialized("ExistingBlogging" + (async ? "Async" : ""))) { using (var context = new OracleDatabaseCreatorTest.BloggingContext(testDatabase)) { var creator = OracleDatabaseCreatorTest.GetDatabaseCreator(context); if (async) { await creator.CreateTablesAsync(); } else { creator.CreateTables(); } if (testDatabase.ConnectionState != ConnectionState.Open) { await testDatabase.OpenConnectionAsync(); } var tables = (await testDatabase.QueryAsync <string>( "SELECT table_name FROM user_tables")).ToList(); Assert.Equal(1, tables.Count); Assert.Equal("Blogs", tables.Single()); var columns = (await testDatabase.QueryAsync <string>( "SELECT table_name || '.' || column_name || ' (' || data_type || ')' " + "FROM user_tab_columns WHERE table_name = 'Blogs' " + "ORDER BY table_name, column_name")).ToArray(); Assert.Equal(14, columns.Length); Assert.Equal( new[] { "Blogs.AndChew (BLOB)", "Blogs.AndRow (RAW)", "Blogs.Cheese (NVARCHAR2)", "Blogs.ErMilan (NUMBER)", "Blogs.Fuse (NUMBER)", "Blogs.George (NUMBER)", "Blogs.Key1 (NVARCHAR2)", "Blogs.Key2 (RAW)", "Blogs.NotFigTime (TIMESTAMP(6))", "Blogs.On (FLOAT)", "Blogs.OrNothing (FLOAT)", "Blogs.TheGu (RAW)", "Blogs.ToEat (NUMBER)", "Blogs.WayRound (NUMBER)" }, columns); } } }
private static async Task Returns_true_when_database_exists_and_has_any_tables_test(bool async) { using (var testDatabase = OracleTestStore.GetOrCreate("ExistingTables") .InitializeOracle(null, t => new OracleDatabaseCreatorTest.BloggingContext(t), null)) { var creator = OracleDatabaseCreatorTest.GetDatabaseCreator(testDatabase); Assert.True(async ? await creator.HasTablesAsyncBase() : creator.HasTablesBase()); } }
public void Can_depend_on_non_generic_options_when_only_one_context_with_default_service_provider() { using (OracleTestStore.GetNorthwindStore()) { using (var context = new NonGenericOptionsContext(new DbContextOptions <DbContext>())) { Assert.True(context.Customers.Any()); } } }
public void Can_specify_connection_in_OnConfiguring_with_default_service_provider() { using (OracleTestStore.GetNorthwindStore()) { using (var context = new ConnectionInOnConfiguringContext(new OracleConnection(OracleNorthwindTestStoreFactory.NorthwindConnectionString))) { Assert.True(context.Customers.Any()); } } }
public async Task Can_query_with_implicit_services_and_OnConfiguring() { using (OracleTestStore.GetNorthwindStore()) { using (var context = new NorthwindContext()) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
public async Task Can_pass_connection_string_to_constructor_and_use_in_OnConfiguring() { using (OracleTestStore.GetNorthwindStore()) { using (var context = new NorthwindContext(OracleNorthwindTestStoreFactory.NorthwindConnectionString)) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
private static async Task Returns_true_when_database_exists_and_has_any_tables_test(bool async) { using (var testDatabase = OracleTestStore.CreateScratch()) { await testDatabase.ExecuteNonQueryAsync("CREATE TABLE SomeTable (Id NUMBER)"); var creator = OracleDatabaseCreatorTest.GetDatabaseCreator(testDatabase); Assert.True(async ? await creator.HasTablesAsyncBase() : creator.HasTablesBase()); } }
public NavigationTestFixture() { var serviceProvider = new ServiceCollection() .AddEntityFrameworkOracle() .BuildServiceProvider(validateScopes: true); _options = new DbContextOptionsBuilder() .UseOracle(OracleTestStore.CreateConnectionString("StateManagerBug"), b => b.ApplyConfiguration()) .UseInternalServiceProvider(serviceProvider) .Options; }
public void Can_depend_on_DbContextOptions_with_default_service_provider() { using (OracleTestStore.GetNorthwindStore()) { using (var context = new OptionsContext( new DbContextOptions <OptionsContext>(), new OracleConnection(OracleNorthwindTestStoreFactory.NorthwindConnectionString))) { Assert.True(context.Customers.Any()); } } }
public async Task Can_pass_context_options_to_constructor_and_use_in_builder() { using (OracleTestStore.GetNorthwindStore()) { using (var context = new NorthwindContext( new DbContextOptionsBuilder() .UseOracle(OracleNorthwindTestStoreFactory.NorthwindConnectionString, b => b.ApplyConfiguration()).Options)) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
public async Task Can_query_with_implicit_services_and_explicit_config() { using (OracleTestStore.GetNorthwindStore()) { using (var context = new NorthwindContext( new DbContextOptionsBuilder() .UseOracle(OracleNorthwindTestStoreFactory.NorthwindConnectionString, b => b.ApplyConfiguration()).Options)) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
private static async Task Returns_false_when_database_does_not_exist_test(bool async) { using (var testDatabase = OracleTestStore.CreateScratch(createDatabase: false)) { using (var context = new OracleDatabaseCreatorTest.BloggingContext(testDatabase)) { var creator = OracleDatabaseCreatorTest.GetDatabaseCreator(context); Assert.False(async ? await creator.ExistsAsync() : creator.Exists()); Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State); } } }
public void Insert_with_non_key_default_value() { using (var testStore = OracleTestStore.CreateInitialized(DatabaseName)) { using (var context = new BlogContextNonKeyDefaultValue(testStore.Name)) { context.Database.EnsureCreated(); var blogs = new List <Blog> { new Blog { Name = "One Unicorn" }, new Blog { Name = "Two Unicorns", CreatedOn = new DateTime(1969, 8, 3, 0, 10, 0) } }; context.AddRange(blogs); context.SaveChanges(); Assert.NotEqual(new DateTime(), blogs[0].CreatedOn); Assert.NotEqual(new DateTime(), blogs[1].CreatedOn); Assert.Null(blogs[0].OtherId); Assert.Null(blogs[1].OtherId); } using (var context = new BlogContextNonKeyDefaultValue(testStore.Name)) { var blogs = context.Blogs.OrderBy(e => e.Name).ToList(); Assert.NotEqual(new DateTime(), blogs[0].CreatedOn); Assert.Equal(new DateTime(1969, 8, 3, 0, 10, 0), blogs[1].CreatedOn); blogs[0].CreatedOn = new DateTime(1973, 9, 3, 0, 10, 0); blogs[1].Name = "Zwo Unicorns"; context.SaveChanges(); } using (var context = new BlogContextNonKeyDefaultValue(testStore.Name)) { var blogs = context.Blogs.OrderBy(e => e.Name).ToList(); Assert.Equal(new DateTime(1969, 8, 3, 0, 10, 0), blogs[1].CreatedOn); Assert.Equal(new DateTime(1973, 9, 3, 0, 10, 0), blogs[0].CreatedOn); } } }
private static async Task Returns_true_when_database_exists_test(bool async) { using (var testDatabase = OracleTestStore.GetOrCreateInitialized("ExistingBlogging")) { using (var context = new OracleDatabaseCreatorTest.BloggingContext(testDatabase)) { var creator = OracleDatabaseCreatorTest.GetDatabaseCreator(context); Assert.True(async ? await creator.ExistsAsync() : creator.Exists()); Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State); } } }