Exemple #1
0
            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]);
                        }
                    }
                }
            }
        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());
                    }
                }
            }
        }
Exemple #3
0
        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);
                }
            }
        }
Exemple #4
0
 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 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 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());
         }
     }
 }
Exemple #7
0
 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());
         }
     }
 }
Exemple #8
0
 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 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());
         }
     }
 }
Exemple #10
0
 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());
         }
     }
 }
Exemple #11
0
            public async Task Can_register_context_and_configuration_with_DI_container_and_have_both_injected()
            {
                var serviceProvider = new ServiceCollection()
                                      .AddTransient <MyController>()
                                      .AddTransient <NorthwindContext>()
                                      .AddSingleton(
                    new DbContextOptionsBuilder()
                    .UseOracle(OracleNorthwindTestStoreFactory.NorthwindConnectionString, b => b.ApplyConfiguration()).Options).BuildServiceProvider();

                using (OracleTestStore.GetNorthwindStore())
                {
                    await serviceProvider.GetRequiredService <MyController>().TestAsync();
                }
            }
Exemple #12
0
            public async Task Can_register_context_with_DI_container_and_have_it_injected()
            {
                var serviceProvider = new ServiceCollection()
                                      .AddEntityFrameworkOracle()
                                      .AddTransient <NorthwindContext>()
                                      .AddTransient <MyController>()
                                      .AddSingleton(p => new DbContextOptionsBuilder().UseInternalServiceProvider(p).Options)
                                      .BuildServiceProvider();

                using (OracleTestStore.GetNorthwindStore())
                {
                    await serviceProvider.GetRequiredService <MyController>().TestAsync();
                }
            }
Exemple #13
0
 public async Task Can_query_with_explicit_services_and_OnConfiguring()
 {
     using (OracleTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext(
                    new DbContextOptionsBuilder().UseInternalServiceProvider(
                        new ServiceCollection()
                        .AddEntityFrameworkOracle()
                        .BuildServiceProvider()).Options))
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
        public void Can_specify_connection_in_OnConfiguring()
        {
            var serviceProvider
                = new ServiceCollection()
                  .AddScoped(_ => new OracleConnection(OracleNorthwindTestStoreFactory.NorthwindConnectionString))
                  .AddDbContext <ConnectionInOnConfiguringContext>().BuildServiceProvider();

            using (OracleTestStore.GetNorthwindStore())
            {
                using (var context = serviceProvider.GetRequiredService <ConnectionInOnConfiguringContext>())
                {
                    Assert.True(context.Customers.Any());
                }
            }
        }
        public void Can_depend_on_non_generic_options_when_only_one_context()
        {
            var serviceProvider
                = new ServiceCollection()
                  .AddDbContext <NonGenericOptionsContext>()
                  .BuildServiceProvider();

            using (OracleTestStore.GetNorthwindStore())
            {
                using (var context = serviceProvider.GetRequiredService <NonGenericOptionsContext>())
                {
                    Assert.True(context.Customers.Any());
                }
            }
        }
Exemple #16
0
        public MappingQueryOracleFixture()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkOracle()
                                  .AddSingleton <ILoggerFactory>(TestSqlLoggerFactory)
                                  .BuildServiceProvider(validateScopes: true);

            _testDatabase = OracleTestStore.GetNorthwindStore();

            _options = new DbContextOptionsBuilder()
                       .UseModel(CreateModel())
                       .UseOracle(_testDatabase.ConnectionString, b => b.ApplyConfiguration())
                       .UseInternalServiceProvider(serviceProvider)
                       .Options;
        }
Exemple #17
0
 public void Throws_on_attempt_to_use_context_with_no_store()
 {
     using (OracleTestStore.GetNorthwindStore())
     {
         Assert.Equal(
             CoreStrings.NoProviderConfigured,
             Assert.Throws <InvalidOperationException>(
                 () =>
         {
             using (var context = new NorthwindContext())
             {
                 Assert.Equal(91, context.Customers.Count());
             }
         }).Message);
     }
 }
        public void Can_depend_on_DbContextOptions()
        {
            var serviceProvider
                = new ServiceCollection()
                  .AddScoped(_ => new OracleConnection(OracleNorthwindTestStoreFactory.NorthwindConnectionString))
                  .AddDbContext <OptionsContext>()
                  .BuildServiceProvider();

            using (OracleTestStore.GetNorthwindStore())
            {
                using (var context = serviceProvider.GetRequiredService <OptionsContext>())
                {
                    Assert.True(context.Customers.Any());
                }
            }
        }
        public async Task Tracking_entities_asynchronously_returns_tracked_entities_back()
        {
            using (var testStore = OracleTestStore.GetNorthwindStore())
            {
                using (var db = new NorthwindContext(Fixture.CreateOptions(testStore)))
                {
                    var customer = await db.Customers.OrderBy(c => c.CustomerID).FirstOrDefaultAsync();

                    var trackedCustomerEntry = db.ChangeTracker.Entries().Single();
                    Assert.Same(trackedCustomerEntry.Entity, customer);

                    // if references are different this will throw
                    db.Customers.Remove(customer);
                }
            }
        }
        public void Can_enumerate_entity_set()
        {
            using (var testStore = OracleTestStore.GetNorthwindStore())
            {
                using (var db = new NorthwindContext(Fixture.CreateOptions(testStore)))
                {
                    var results = new List <Customer>();
                    foreach (var item in db.Customers)
                    {
                        results.Add(item);
                    }

                    Assert.Equal(91, results.Count);
                    Assert.Equal("ALFKI", results[0].CustomerID);
                    Assert.Equal("Alfreds Futterkiste", results[0].CompanyName);
                }
            }
        }
Exemple #21
0
 public void Throws_on_attempt_to_use_SQL_Server_without_providing_connection_string()
 {
     using (OracleTestStore.GetNorthwindStore())
     {
         Assert.Equal(
             CoreStrings.NoProviderConfigured,
             Assert.Throws <InvalidOperationException>(
                 () =>
         {
             using (var context = new NorthwindContext(
                        new DbContextOptionsBuilder().UseInternalServiceProvider(
                            new ServiceCollection()
                            .AddEntityFrameworkOracle()
                            .BuildServiceProvider()).Options))
             {
                 Assert.Equal(91, context.Customers.Count());
             }
         }).Message);
     }
 }
Exemple #22
0
            public void Throws_on_attempt_to_use_store_with_no_store_services()
            {
                var serviceCollection = new ServiceCollection();

                new EntityFrameworkServicesBuilder(serviceCollection).TryAddCoreServices();
                var serviceProvider = serviceCollection.BuildServiceProvider();

                using (OracleTestStore.GetNorthwindStore())
                {
                    Assert.Equal(
                        CoreStrings.NoProviderConfigured,
                        Assert.Throws <InvalidOperationException>(
                            () =>
                    {
                        using (var context = new NorthwindContext(
                                   new DbContextOptionsBuilder()
                                   .UseInternalServiceProvider(serviceProvider).Options))
                        {
                            Assert.Equal(91, context.Customers.Count());
                        }
                    }).Message);
                }
            }
        public void Can_run_linq_query_on_entity_set_with_value_buffer_reader()
        {
            using (var testStore = OracleTestStore.GetNorthwindStore())
            {
                using (var db = new NorthwindContext(Fixture.CreateOptions(testStore)))
                {
                    var results = db.Customers
                                  .Where(c => c.CompanyName.StartsWith("A"))
                                  .OrderByDescending(c => c.CustomerID)
                                  .ToList();

                    Assert.Equal(4, results.Count);
                    Assert.Equal("AROUT", results[0].CustomerID);
                    Assert.Equal("ANTON", results[1].CustomerID);
                    Assert.Equal("ANATR", results[2].CustomerID);
                    Assert.Equal("ALFKI", results[3].CustomerID);

                    Assert.Equal("(171) 555-6750", results[0].Fax);
                    Assert.Null(results[1].Fax);
                    Assert.Equal("(5) 555-3745", results[2].Fax);
                    Assert.Equal("030-0076545", results[3].Fax);
                }
            }
        }