public async Task Can_use_one_context_nested_inside_another_of_the_same_type() { using (SqlCeNorthwindContext.GetSharedStore()) { var serviceProvider = new ServiceCollection() .AddEntityFrameworkSqlCe() .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 IServiceProvider BuildServiceProvider <TContext>(int poolSize = 32) where TContext : DbContext => new ServiceCollection() .AddEntityFrameworkSqlCe() .AddDbContextPool <TContext>( ob => ob.UseSqlCe(SqlCeNorthwindContext.GetSharedStore().ConnectionString), poolSize) .BuildServiceProvider();
public async Task Can_pass_connection_string_to_constructor_and_use_in_OnConfiguring() { using (SqlCeNorthwindContext.GetSharedStore()) { using (var context = new NorthwindContext(SqlCeNorthwindContext.ConnectionString)) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
public async Task Can_query_with_implicit_services_and_OnConfiguring() { using (SqlCeNorthwindContext.GetSharedStore()) { using (var context = new NorthwindContext()) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
public async Task Can_pass_context_options_to_constructor_and_use_in_builder() { using (SqlCeNorthwindContext.GetSharedStore()) { using (var context = new NorthwindContext(new DbContextOptionsBuilder() .UseSqlCe(SqlCeNorthwindContext.ConnectionString, b => b.ApplyConfiguration()).Options)) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
public async Task Can_query_with_implicit_services_and_explicit_config() { using (SqlCeNorthwindContext.GetSharedStore()) { using (var context = new NorthwindContext( new DbContextOptionsBuilder() .UseSqlCe(SqlCeNorthwindContext.ConnectionString, b => b.ApplyConfiguration()).Options)) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
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() .UseSqlCe(SqlCeNorthwindContext.ConnectionString, b => b.ApplyConfiguration()).Options).BuildServiceProvider(); using (SqlCeNorthwindContext.GetSharedStore()) { await serviceProvider.GetRequiredService <MyController>().TestAsync(); } }
public async Task Can_query_with_explicit_services_and_OnConfiguring() { using (SqlCeNorthwindContext.GetSharedStore()) { using (var context = new NorthwindContext( new DbContextOptionsBuilder().UseInternalServiceProvider( new ServiceCollection() .AddEntityFrameworkSqlCe() .BuildServiceProvider()).Options)) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
public async Task Can_register_context_with_DI_container_and_have_it_injected() { var serviceProvider = new ServiceCollection() .AddEntityFrameworkSqlCe() .AddTransient <NorthwindContext>() .AddTransient <MyController>() .AddSingleton(p => new DbContextOptionsBuilder().UseInternalServiceProvider(p).Options) .BuildServiceProvider(); using (SqlCeNorthwindContext.GetSharedStore()) { await serviceProvider.GetRequiredService <MyController>().TestAsync(); } }
public void Throws_on_attempt_to_use_context_with_no_store() { using (SqlCeNorthwindContext.GetSharedStore()) { Assert.Equal( CoreStrings.NoProviderConfigured, Assert.Throws <InvalidOperationException>(() => { using (var context = new NorthwindContext()) { Assert.Equal(91, context.Customers.Count()); } }).Message); } }
public MappingQuerySqlCeFixture() { var serviceProvider = new ServiceCollection() .AddEntityFrameworkSqlCe() .AddSingleton <ILoggerFactory>(TestSqlLoggerFactory) .BuildServiceProvider(); _testDatabase = SqlCeNorthwindContext.GetSharedStore(); var optionsBuilder = new DbContextOptionsBuilder().UseModel(CreateModel()); optionsBuilder .UseSqlCe(_testDatabase.Connection.ConnectionString) .UseInternalServiceProvider(serviceProvider); _options = optionsBuilder.Options; }
public void Throws_on_attempt_to_use_SQL_Server_without_providing_connection_string() { using (SqlCeNorthwindContext.GetSharedStore()) { Assert.Equal( CoreStrings.NoProviderConfigured, Assert.Throws <InvalidOperationException>(() => { using (var context = new NorthwindContext( new DbContextOptionsBuilder().UseInternalServiceProvider(new ServiceCollection() .AddEntityFrameworkSqlCe() .BuildServiceProvider()).Options)) { Assert.Equal(91, context.Customers.Count()); } }).Message); } }
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 (SqlCeNorthwindContext.GetSharedStore()) { Assert.Equal( CoreStrings.NoProviderConfigured, Assert.Throws <InvalidOperationException>(() => { using (var context = new NorthwindContext( new DbContextOptionsBuilder() .UseInternalServiceProvider(serviceProvider).Options)) { Assert.Equal(91, context.Customers.Count()); } }).Message); } }