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, MySqlNorthwindTestStoreFactory.NorthwindConnectionString }
            });

            var serviceProvider
                = new ServiceCollection()
                  .AddSingleton <IConfiguration>(configBuilder.Build())
                  .AddDbContext <UseConfigurationContext>(
                      b => b.UseMySql(connectionString))
                  .BuildServiceProvider();

            using (MySqlTestStore.GetNorthwindStore())
            {
                using (var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
                {
                    using (var context = serviceScope.ServiceProvider.GetRequiredService <UseConfigurationContext>())
                    {
                        Assert.True(context.Customers.Any());
                    }
                }
            }
        }
Пример #2
0
            public async Task Can_use_one_context_nested_inside_another_of_the_same_type()
            {
                using (MySqlTestStore.GetNorthwindStore())
                {
                    var serviceProvider = new ServiceCollection()
                                          .AddEntityFrameworkMySql()
                                          .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 Can_use_an_existing_closed_connection_test(bool openConnection)
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkMySql()
                                  .BuildServiceProvider();

            using (var store = MySqlTestStore.GetNorthwindStore())
            {
                store.CloseConnection();

                var openCount    = 0;
                var closeCount   = 0;
                var disposeCount = 0;

                using (var connection = new SqlConnection(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);
                }
            }
        }
Пример #4
0
 public async Task Can_pass_connection_string_to_constructor_and_use_in_OnConfiguring()
 {
     using (MySqlTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext(MySqlNorthwindTestStoreFactory.NorthwindConnectionString))
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
 public void Can_depend_on_non_generic_options_when_only_one_context_with_default_service_provider()
 {
     using (MySqlTestStore.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 (MySqlTestStore.GetNorthwindStore())
     {
         using (var context = new ConnectionInOnConfiguringContext(new SqlConnection(MySqlNorthwindTestStoreFactory.NorthwindConnectionString)))
         {
             Assert.True(context.Customers.Any());
         }
     }
 }
Пример #7
0
 public async Task Can_query_with_implicit_services_and_OnConfiguring()
 {
     using (MySqlTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext())
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
Пример #8
0
 public async Task Can_pass_context_options_to_constructor_and_use_in_builder()
 {
     using (MySqlTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext(
                    new DbContextOptionsBuilder()
                    .UseMySql(MySqlNorthwindTestStoreFactory.NorthwindConnectionString, b => b.ApplyConfiguration()).Options))
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
Пример #9
0
 public async Task Can_query_with_implicit_services_and_explicit_config()
 {
     using (MySqlTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext(
                    new DbContextOptionsBuilder()
                    .UseMySql(MySqlNorthwindTestStoreFactory.NorthwindConnectionString, b => b.ApplyConfiguration()).Options))
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
 public void Can_depend_on_DbContextOptions_with_default_service_provider()
 {
     using (MySqlTestStore.GetNorthwindStore())
     {
         using (var context = new OptionsContext(
                    new DbContextOptions <OptionsContext>(),
                    new SqlConnection(MySqlNorthwindTestStoreFactory.NorthwindConnectionString)))
         {
             Assert.True(context.Customers.Any());
         }
     }
 }
Пример #11
0
            public async Task Can_register_context_with_DI_container_and_have_it_injected()
            {
                var serviceProvider = new ServiceCollection()
                                      .AddEntityFrameworkMySql()
                                      .AddTransient <NorthwindContext>()
                                      .AddTransient <MyController>()
                                      .AddSingleton(p => new DbContextOptionsBuilder().UseInternalServiceProvider(p).Options)
                                      .BuildServiceProvider();

                using (MySqlTestStore.GetNorthwindStore())
                {
                    await serviceProvider.GetRequiredService <MyController>().TestAsync();
                }
            }
Пример #12
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()
                    .UseMySql(MySqlNorthwindTestStoreFactory.NorthwindConnectionString, b => b.ApplyConfiguration()).Options).BuildServiceProvider();

                using (MySqlTestStore.GetNorthwindStore())
                {
                    await serviceProvider.GetRequiredService <MyController>().TestAsync();
                }
            }
Пример #13
0
 public async Task Can_query_with_explicit_services_and_OnConfiguring()
 {
     using (MySqlTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext(
                    new DbContextOptionsBuilder().UseInternalServiceProvider(
                        new ServiceCollection()
                        .AddEntityFrameworkMySql()
                        .BuildServiceProvider()).Options))
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
        public void Can_specify_connection_in_OnConfiguring()
        {
            var serviceProvider
                = new ServiceCollection()
                  .AddScoped(p => new SqlConnection(MySqlNorthwindTestStoreFactory.NorthwindConnectionString))
                  .AddDbContext <ConnectionInOnConfiguringContext>().BuildServiceProvider();

            using (MySqlTestStore.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 (MySqlTestStore.GetNorthwindStore())
            {
                using (var context = serviceProvider.GetRequiredService <NonGenericOptionsContext>())
                {
                    Assert.True(context.Customers.Any());
                }
            }
        }
        public void Can_depend_on_DbContextOptions()
        {
            var serviceProvider
                = new ServiceCollection()
                  .AddScoped(p => new SqlConnection(MySqlNorthwindTestStoreFactory.NorthwindConnectionString))
                  .AddDbContext <OptionsContext>()
                  .BuildServiceProvider();

            using (MySqlTestStore.GetNorthwindStore())
            {
                using (var context = serviceProvider.GetRequiredService <OptionsContext>())
                {
                    Assert.True(context.Customers.Any());
                }
            }
        }
Пример #17
0
 public void Throws_on_attempt_to_use_context_with_no_store()
 {
     using (MySqlTestStore.GetNorthwindStore())
     {
         Assert.Equal(
             CoreStrings.NoProviderConfigured,
             Assert.Throws <InvalidOperationException>(
                 () =>
         {
             using (var context = new NorthwindContext())
             {
                 Assert.Equal(91, context.Customers.Count());
             }
         }).Message);
     }
 }
Пример #18
0
 public void Throws_on_attempt_to_use_SQL_Server_without_providing_connection_string()
 {
     using (MySqlTestStore.GetNorthwindStore())
     {
         Assert.Equal(
             CoreStrings.NoProviderConfigured,
             Assert.Throws <InvalidOperationException>(
                 () =>
         {
             using (var context = new NorthwindContext(
                        new DbContextOptionsBuilder().UseInternalServiceProvider(
                            new ServiceCollection()
                            .AddEntityFrameworkMySql()
                            .BuildServiceProvider()).Options))
             {
                 Assert.Equal(91, context.Customers.Count());
             }
         }).Message);
     }
 }
Пример #19
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 (MySqlTestStore.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);
                }
            }