Beispiel #1
0
        public async void Can_specify_connection_in_OnConfiguring()
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection
                .AddScoped<SqlConnection>(p => new SqlConnection(SqlServerNorthwindContext.ConnectionString))
                .AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<ConnectionInOnConfiguringContext>();

            var serviceProvider = serviceCollection.BuildServiceProvider();

            using (await SqlServerNorthwindContext.GetSharedStoreAsync())
            {
                using (var context = serviceProvider.GetRequiredService<ConnectionInOnConfiguringContext>())
                {
                    Assert.True(context.Customers.Any());
                }
            }
        }
Beispiel #2
0
        public async void Can_select_appropriate_provider_when_multiple_registered_with_default_service_provider()
        {
            using (await SqlServerNorthwindContext.GetSharedStoreAsync())
            {
                using (var context = new MultipleProvidersContext())
                {
                    context.UseSqlServer = true;

                    Assert.True(context.Customers.Any());
                }

                using (var context = new MultipleProvidersContext())
                {
                    context.UseSqlServer = false;

                    Assert.False(context.Customers.Any());
                }
            }
        }
Beispiel #3
0
        public async void Can_depend_on_non_generic_options_when_only_one_context()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection
            .AddEntityFramework()
            .AddSqlServer()
            .AddInMemoryStore()
            .AddDbContext <NonGenericOptionsContext>();

            var serviceProvider = serviceCollection.BuildServiceProvider();

            using (await SqlServerNorthwindContext.GetSharedStoreAsync())
            {
                using (var context = serviceProvider.GetRequiredService <NonGenericOptionsContext>())
                {
                    Assert.True(context.Customers.Any());
                }
            }
        }
Beispiel #4
0
        public async void Can_depend_on_DbContextOptions()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection
            .AddScoped(p => new SqlConnection(SqlServerNorthwindContext.ConnectionString))
            .AddEntityFramework()
            .AddSqlServer()
            .AddDbContext <OptionsContext>();

            var serviceProvider = serviceCollection.BuildServiceProvider();

            using (await SqlServerNorthwindContext.GetSharedStoreAsync())
            {
                using (var context = serviceProvider.GetRequiredService <OptionsContext>())
                {
                    Assert.True(context.Customers.Any());
                }
            }
        }
            public async Task Can_query_with_explicit_services_and_explicit_config()
            {
                using (await SqlServerNorthwindContext.GetSharedStoreAsync())
                {
                    var serviceCollection = new ServiceCollection();
                    serviceCollection
                    .AddEntityFramework()
                    .AddSqlServer();

                    var serviceProvider = serviceCollection.BuildServiceProvider();

                    var optionsBuilder = new DbContextOptionsBuilder();
                    optionsBuilder.UseSqlServer(SqlServerNorthwindContext.ConnectionString);

                    using (var context = new NorthwindContext(serviceProvider, optionsBuilder.Options))
                    {
                        Assert.Equal(91, await context.Customers.CountAsync());
                    }
                }
            }
            public async Task Throws_on_attempt_to_use_store_with_no_store_services()
            {
                using (await SqlServerNorthwindContext.GetSharedStoreAsync())
                {
                    var serviceCollection = new ServiceCollection();
                    serviceCollection
                    .AddEntityFramework();

                    var serviceProvider = serviceCollection.BuildServiceProvider();

                    Assert.Equal(
                        CoreStrings.NoDataStoreService,
                        Assert.Throws <InvalidOperationException>(() =>
                    {
                        using (var context = new NorthwindContext(serviceProvider))
                        {
                            Assert.Equal(91, context.Customers.Count());
                        }
                    }).Message);
                }
            }
Beispiel #7
0
            public async Task Can_register_configuration_with_DI_container_and_have_it_injected()
            {
                var options = new DbContextOptions().UseSqlServer(SqlServerNorthwindContext.ConnectionString);

                var serviceCollection = new ServiceCollection();

                serviceCollection
                .AddEntityFramework()
                .AddSqlServer();

                serviceCollection
                .AddTransient <NorthwindContext>()
                .AddTransient <MyController>()
                .AddInstance(options);
                var serviceProvider = serviceCollection.BuildServiceProvider();

                using (await SqlServerNorthwindContext.GetSharedStoreAsync())
                {
                    await serviceProvider.GetRequiredService <MyController>().TestAsync();
                }
            }
Beispiel #8
0
            public async Task Throws_on_attempt_to_use_SQL_Server_without_providing_connection_string()
            {
                using (await SqlServerNorthwindContext.GetSharedStoreAsync())
                {
                    var serviceCollection = new ServiceCollection();
                    serviceCollection
                    .AddEntityFramework()
                    .AddSqlServer();

                    var serviceProvider = serviceCollection.BuildServiceProvider();

                    Assert.Equal(
                        RelationalStrings.NoConnectionOrConnectionString,
                        Assert.Throws <InvalidOperationException>(() =>
                    {
                        using (var context = new NorthwindContext(serviceProvider))
                        {
                            Assert.Equal(91, context.Customers.Count());
                        }
                    }).Message);
                }
            }
            private async Task NestedContextTest(Func <BlogContext> createBlogContext, Func <NorthwindContext> createNorthwindContext)
            {
                using (await SqlServerNorthwindContext.GetSharedStoreAsync())
                {
                    using (var context0 = createBlogContext())
                    {
                        Assert.Equal(0, context0.ChangeTracker.Entries().Count());
                        var blog0 = context0.Add(new Blog {
                            Id = 1, Name = "Giddyup"
                        }).Entity;
                        Assert.Same(blog0, context0.ChangeTracker.Entries().Select(e => e.Entity).Single());
                        await context0.SaveChangesAsync();

                        using (var context1 = createNorthwindContext())
                        {
                            var customers1 = await context1.Customers.ToListAsync();

                            Assert.Equal(91, customers1.Count);
                            Assert.Equal(91, context1.ChangeTracker.Entries().Count());
                            Assert.Same(blog0, context0.ChangeTracker.Entries().Select(e => e.Entity).Single());

                            using (var context2 = createBlogContext())
                            {
                                Assert.Equal(0, context2.ChangeTracker.Entries().Count());
                                Assert.Same(blog0, context0.ChangeTracker.Entries().Select(e => e.Entity).Single());

                                var blog0Prime = (await context2.Blogs.ToArrayAsync()).Single();
                                Assert.Same(blog0Prime, context2.ChangeTracker.Entries().Select(e => e.Entity).Single());

                                Assert.Equal(blog0.Id, blog0Prime.Id);
                                Assert.NotSame(blog0, blog0Prime);
                            }
                        }
                    }
                }
            }
        public async Task Can_run_linq_query_on_entity_set_with_value_buffer_reader()
        {
            using (await SqlServerNorthwindContext.GetSharedStoreAsync())
            {
                var serviceCollection = new ServiceCollection();
                serviceCollection
                .AddEntityFramework()
                .AddSqlServer();

                serviceCollection.AddScoped <SqlServerDataStore, SqlStoreWithBufferReader>();
                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);

                    var contextServices = ((IDbContextServices)db).ScopedServiceProvider;

                    Assert.IsType <SqlStoreWithBufferReader>(contextServices.GetRequiredService <DbContextService <DataStore> >().Service);
                }
            }
        }
        public async Task Can_run_linq_query_on_entity_set()
        {
            using (await SqlServerNorthwindContext.GetSharedStoreAsync())
            {
                using (var db = new NorthwindContext(_fixture.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);
                }
            }
        }