public void Can_run_linq_query_on_entity_set_with_value_buffer_reader() { using (SqlServerNorthwindContext.GetSharedStore()) { var serviceCollection = new ServiceCollection(); serviceCollection .AddEntityFrameworkSqlServer(); serviceCollection.AddSingleton <IRelationalValueBufferFactoryFactory, TestTypedValueBufferFactoryFactory>(); var serviceProvider = serviceCollection.BuildServiceProvider(); using (var db = new NorthwindContext(serviceProvider)) { 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); } } }
public async Task Can_use_one_context_nested_inside_another_of_the_same_type() { using (SqlServerNorthwindContext.GetSharedStore()) { var serviceProvider = new ServiceCollection() .AddEntityFrameworkSqlServer() .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 serviceCollection = new ServiceCollection(); serviceCollection .AddEntityFramework() .AddSqlServer(); var serviceProvider = serviceCollection.BuildServiceProvider(); using (var store = SqlServerNorthwindContext.GetSharedStore()) { var openCount = 0; var closeCount = 0; var disposeCount = 0; using (var connection = new SqlConnection(store.Connection.ConnectionString)) { if (openConnection) { await connection.OpenAsync(); } connection.StateChange += (_, a) => { if (a.CurrentState == ConnectionState.Open) { openCount++; } else if (a.CurrentState == ConnectionState.Closed) { closeCount++; } }; #if !DNXCORE50 connection.Disposed += (_, __) => disposeCount++; #endif 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_use_one_context_nested_inside_another_of_a_different_type_with_implicit_services() { using (SqlServerNorthwindContext.GetSharedStore()) { await NestedContextTest(() => new BlogContext(), () => new NorthwindContext()); } }
public void Can_register_multiple_context_types() { var serviceCollection = new ServiceCollection(); serviceCollection .AddEntityFramework() .AddSqlServer() .AddInMemoryDatabase() .AddDbContext <MultipleContext1>() .AddDbContext <MultipleContext2>(); var serviceProvider = serviceCollection.BuildServiceProvider(); using (SqlServerNorthwindContext.GetSharedStore()) { using (var context = serviceProvider.GetRequiredService <MultipleContext1>()) { Assert.True(context.Customers.Any()); } using (var context = serviceProvider.GetRequiredService <MultipleContext2>()) { Assert.False(context.Customers.Any()); } } }
private static IServiceProvider BuildServiceProvider <TContext>(int poolSize = 32) where TContext : DbContext => new ServiceCollection() .AddEntityFrameworkSqlServer() .AddDbContextPool <TContext>( ob => ob.UseSqlServer(SqlServerNorthwindContext.GetSharedStore().ConnectionString), poolSize) .BuildServiceProvider();
public void Can_select_appropriate_provider_when_multiple_registered() { var serviceCollection = new ServiceCollection(); serviceCollection .AddScoped <SomeService>() .AddEntityFramework() .AddSqlServer() .AddInMemoryDatabase() .AddDbContext <MultipleProvidersContext>(); var serviceProvider = serviceCollection.BuildServiceProvider(); using (SqlServerNorthwindContext.GetSharedStore()) { MultipleProvidersContext context1; MultipleProvidersContext context2; using (var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope()) { using (context1 = serviceScope.ServiceProvider.GetRequiredService <MultipleProvidersContext>()) { context1.UseSqlServer = true; Assert.True(context1.Customers.Any()); } using (var context1B = serviceScope.ServiceProvider.GetRequiredService <MultipleProvidersContext>()) { Assert.Same(context1, context1B); } var someService = serviceScope.ServiceProvider.GetRequiredService <SomeService>(); Assert.Same(context1, someService.Context); } using (var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope()) { using (context2 = serviceScope.ServiceProvider.GetRequiredService <MultipleProvidersContext>()) { context2.UseSqlServer = false; Assert.False(context2.Customers.Any()); } using (var context2B = serviceScope.ServiceProvider.GetRequiredService <MultipleProvidersContext>()) { Assert.Same(context2, context2B); } var someService = serviceScope.ServiceProvider.GetRequiredService <SomeService>(); Assert.Same(context2, someService.Context); } Assert.NotSame(context1, context2); } }
public async Task Can_pass_connection_string_to_constructor_and_use_in_OnConfiguring() { using (SqlServerNorthwindContext.GetSharedStore()) { using (var context = new NorthwindContext(SqlServerNorthwindContext.ConnectionString)) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
public async Task Can_query_with_implicit_services_and_OnConfiguring() { using (SqlServerNorthwindContext.GetSharedStore()) { using (var context = new NorthwindContext()) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
public void Can_depend_on_non_generic_options_when_only_one_context_with_default_service_provider() { using (SqlServerNorthwindContext.GetSharedStore()) { 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 (SqlServerNorthwindContext.GetSharedStore()) { using (var context = new ConnectionInOnConfiguringContext(new SqlConnection(SqlServerNorthwindContext.ConnectionString))) { Assert.True(context.Customers.Any()); } } }
public async Task Can_pass_context_options_to_constructor_and_use_in_builder() { using (SqlServerNorthwindContext.GetSharedStore()) { using (var context = new NorthwindContext(new DbContextOptionsBuilder() .UseSqlServer(SqlServerNorthwindContext.ConnectionString, b => b.ApplyConfiguration()).Options)) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
public async Task Can_query_with_implicit_services_and_explicit_config() { using (SqlServerNorthwindContext.GetSharedStore()) { using (var context = new NorthwindContext( new DbContextOptionsBuilder() .UseSqlServer(SqlServerNorthwindContext.ConnectionString, b => b.ApplyConfiguration()).Options)) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
public async Task Can_use_one_context_nested_inside_another_of_a_different_type() { using (SqlServerNorthwindContext.GetSharedStore()) { var serviceProvider = new ServiceCollection() .AddEntityFrameworkSqlServer() .AddEntityFrameworkInMemoryDatabase() .BuildServiceProvider(); await NestedContextTest(() => new BlogContext(serviceProvider), () => new NorthwindContext(serviceProvider)); } }
public void Can_depend_on_DbContextOptions_with_default_service_provider() { using (SqlServerNorthwindContext.GetSharedStore()) { using (var context = new OptionsContext( new DbContextOptions <OptionsContext>(), new SqlConnection(SqlServerNorthwindContext.ConnectionString))) { Assert.True(context.Customers.Any()); } } }
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() .UseSqlServer(SqlServerNorthwindContext.ConnectionString, b => b.ApplyConfiguration()).Options).BuildServiceProvider(); using (SqlServerNorthwindContext.GetSharedStore()) { await serviceProvider.GetRequiredService <MyController>().TestAsync(); } }
public async Task Can_register_context_with_DI_container_and_have_it_injected() { var serviceProvider = new ServiceCollection() .AddEntityFrameworkSqlServer() .AddTransient <NorthwindContext>() .AddTransient <MyController>() .AddSingleton(p => new DbContextOptionsBuilder().UseInternalServiceProvider(p).Options) .BuildServiceProvider(); using (SqlServerNorthwindContext.GetSharedStore()) { await serviceProvider.GetRequiredService <MyController>().TestAsync(); } }
public async Task Can_query_with_explicit_services_and_OnConfiguring() { using (SqlServerNorthwindContext.GetSharedStore()) { using (var context = new NorthwindContext( new DbContextOptionsBuilder().UseInternalServiceProvider( new ServiceCollection() .AddEntityFrameworkSqlServer() .BuildServiceProvider()).Options)) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
public void Can_register_multiple_context_types_with_default_service_provider() { using (SqlServerNorthwindContext.GetSharedStore()) { using (var context = new MultipleContext1(new DbContextOptions <MultipleContext1>())) { Assert.True(context.Customers.Any()); } using (var context = new MultipleContext2(new DbContextOptions <MultipleContext2>())) { Assert.False(context.Customers.Any()); } } }
public void Can_depend_on_non_generic_options_when_only_one_context() { var serviceProvider = new ServiceCollection() .AddDbContext <NonGenericOptionsContext>() .BuildServiceProvider(); using (SqlServerNorthwindContext.GetSharedStore()) { using (var context = serviceProvider.GetRequiredService <NonGenericOptionsContext>()) { Assert.True(context.Customers.Any()); } } }
public void Can_specify_connection_in_OnConfiguring() { var serviceProvider = new ServiceCollection() .AddScoped(p => new SqlConnection(SqlServerNorthwindContext.ConnectionString)) .AddDbContext <ConnectionInOnConfiguringContext>().BuildServiceProvider(); using (SqlServerNorthwindContext.GetSharedStore()) { using (var context = serviceProvider.GetRequiredService <ConnectionInOnConfiguringContext>()) { Assert.True(context.Customers.Any()); } } }
public void Throws_on_attempt_to_use_context_with_no_store() { using (SqlServerNorthwindContext.GetSharedStore()) { Assert.Equal( CoreStrings.NoProviderConfigured, Assert.Throws <InvalidOperationException>(() => { using (var context = new NorthwindContext()) { Assert.Equal(91, context.Customers.Count()); } }).Message); } }
public MappingQuerySqlServerFixture() { var serviceProvider = new ServiceCollection() .AddEntityFrameworkSqlServer() .AddSingleton <ILoggerFactory>(new TestSqlLoggerFactory()) .BuildServiceProvider(); _testDatabase = SqlServerNorthwindContext.GetSharedStore(); _options = new DbContextOptionsBuilder() .UseModel(CreateModel()) .UseSqlServer(_testDatabase.ConnectionString, b => b.ApplyConfiguration()) .UseInternalServiceProvider(serviceProvider) .Options; }
public void Can_depend_on_DbContextOptions() { var serviceProvider = new ServiceCollection() .AddScoped(p => new SqlConnection(SqlServerNorthwindContext.ConnectionString)) .AddDbContext <OptionsContext>() .BuildServiceProvider(); using (SqlServerNorthwindContext.GetSharedStore()) { using (var context = serviceProvider.GetRequiredService <OptionsContext>()) { Assert.True(context.Customers.Any()); } } }
public MappingQuerySqlServerFixture() { _serviceProvider = new ServiceCollection() .AddEntityFramework() .AddSqlServer() .ServiceCollection() .AddSingleton <ILoggerFactory>(new TestSqlLoggerFactory()) .BuildServiceProvider(); _testDatabase = SqlServerNorthwindContext.GetSharedStore(); var optionsBuilder = new DbContextOptionsBuilder().UseModel(CreateModel()); optionsBuilder.UseSqlServer(_testDatabase.ConnectionString); _options = optionsBuilder.Options; }
public async Task Tracking_entities_asynchronously_returns_tracked_entities_back() { using (SqlServerNorthwindContext.GetSharedStore()) { using (var db = new NorthwindContext(_fixture.ServiceProvider)) { var customer = await db.Customers.FirstOrDefaultAsync(); var trackedCustomerEntry = db.ChangeTracker.Entries().Single(); Assert.Same(trackedCustomerEntry.Entity, customer); // if references are different this will throw db.Customers.Remove(customer); } } }
public NorthwindSprocQuerySqlServerFixture() { _testStore = SqlServerNorthwindContext.GetSharedStore(); var serviceProvider = new ServiceCollection() .AddEntityFrameworkSqlServer() .AddSingleton(TestSqlServerModelSource.GetFactory(OnModelCreating)) .AddSingleton <ILoggerFactory>(new TestSqlLoggerFactory()) .BuildServiceProvider(); _options = new DbContextOptionsBuilder() .EnableSensitiveDataLogging() .UseInternalServiceProvider(serviceProvider) .UseSqlServer(_testStore.ConnectionString).Options; serviceProvider.GetRequiredService <ILoggerFactory>(); }
public async Task Can_query_with_explicit_services_and_OnConfiguring() { using (SqlServerNorthwindContext.GetSharedStore()) { var serviceCollection = new ServiceCollection(); serviceCollection .AddEntityFramework() .AddSqlServer(); var serviceProvider = serviceCollection.BuildServiceProvider(); using (var context = new NorthwindContext(serviceProvider)) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
public void Can_enumerate_entity_set() { using (SqlServerNorthwindContext.GetSharedStore()) { using (var db = new NorthwindContext(_fixture.ServiceProvider)) { 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); } } }
public void Throws_on_attempt_to_use_SQL_Server_without_providing_connection_string() { using (SqlServerNorthwindContext.GetSharedStore()) { Assert.Equal( CoreStrings.NoProviderConfigured, Assert.Throws <InvalidOperationException>(() => { using (var context = new NorthwindContext( new DbContextOptionsBuilder().UseInternalServiceProvider(new ServiceCollection() .AddEntityFrameworkSqlServer() .BuildServiceProvider()).Options)) { Assert.Equal(91, context.Customers.Count()); } }).Message); } }