Exemple #1
0
        public async Task Can_run_linq_query_on_entity_set_with_value_buffer_reader()
        {
            using (await SqlServerTestDatabase.Northwind())
            {
                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);

                    Assert.IsType <SqlStoreWithBufferReader>(db.Configuration.DataStore);
                }
            }
        }
Exemple #2
0
            public async Task Can_use_one_context_nested_inside_another_of_the_same_type()
            {
                using (await SqlServerTestDatabase.Northwind())
                {
                    var serviceCollection = new ServiceCollection();
                    serviceCollection.AddEntityFramework().AddSqlServer();
                    var serviceProvider = serviceCollection.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 async Task Can_use_connection_string_name_in_OnConfiguring_test(string connectionName)
        {
            var configuration = new Configuration
            {
                new MemoryConfigurationSource(
                    new Dictionary <string, string>
                {
                    { "Data:Northwind:ConnectionString", SqlServerTestDatabase.NorthwindConnectionString }
                })
            };

            var serviceCollection = new ServiceCollection();

            serviceCollection
            .AddInstance <IConfiguration>(configuration)
            .AddEntityFramework()
            .AddSqlServer();

            var serviceProvider = serviceCollection.BuildServiceProvider();

            using (await SqlServerTestDatabase.Northwind())
            {
                using (var context = new NorthwindContext(serviceProvider, connectionName))
                {
                    Assert.Equal(91, await context.Customers.CountAsync());
                }
            }
        }
Exemple #4
0
 public async Task Can_use_one_context_nested_inside_another_of_a_different_type_with_implicit_services()
 {
     using (await SqlServerTestDatabase.Northwind())
     {
         await NestedContextTest(() => new BlogContext(), () => new NorthwindContext());
     }
 }
        private static async Task Can_use_an_existing_closed_connection_test(bool openConnection)
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection
            .AddEntityFramework()
            .AddSqlServer()
            .UseLoggerFactory <LoggerFactory>();
            var serviceProvider = serviceCollection.BuildServiceProvider();

            using (await SqlServerTestDatabase.Northwind())
            {
                var openCount    = 0;
                var closeCount   = 0;
                var disposeCount = 0;

                using (var connection = new SqlConnection(SqlServerTestDatabase.NorthwindConnectionString))
                {
                    if (openConnection)
                    {
                        await connection.OpenAsync();
                    }

                    connection.StateChange += (_, a) =>
                    {
                        if (a.CurrentState == ConnectionState.Open)
                        {
                            openCount++;
                        }
                        else if (a.CurrentState == ConnectionState.Closed)
                        {
                            closeCount++;
                        }
                    };
#if !ASPNETCORE50
                    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);
                }
            }
        }
Exemple #6
0
 public async Task Can_query_with_implicit_services_and_OnConfiguring()
 {
     using (await SqlServerTestDatabase.Northwind())
     {
         using (var context = new NorthwindContext())
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
Exemple #7
0
 public async Task Can_pass_connection_string_to_constructor_and_use_in_builder()
 {
     using (await SqlServerTestDatabase.Northwind())
     {
         using (var context = new NorthwindContext(SqlServerTestDatabase.NorthwindConnectionString))
         {
             Assert.Equal(91, await QueryableExtensions.CountAsync(context.Customers));
         }
     }
 }
Exemple #8
0
 public async Task Can_pass_connection_string_to_constructor_and_use_in_OnConfiguring()
 {
     using (await SqlServerTestDatabase.Northwind())
     {
         using (var context = new NorthwindContext(SqlServerTestDatabase.NorthwindConnectionString))
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
Exemple #9
0
            public async Task Can_use_one_context_nested_inside_another_of_a_different_type()
            {
                using (await SqlServerTestDatabase.Northwind())
                {
                    var serviceCollection = new ServiceCollection();
                    serviceCollection.AddEntityFramework().AddSqlServer().AddInMemoryStore();
                    var serviceProvider = serviceCollection.BuildServiceProvider();

                    await NestedContextTest(() => new BlogContext(serviceProvider), () => new NorthwindContext(serviceProvider));
                }
            }
Exemple #10
0
            public async Task Can_query_with_implicit_services_and_explicit_config()
            {
                using (await SqlServerTestDatabase.Northwind())
                {
                    var options = new DbContextOptions().UseSqlServer(SqlServerTestDatabase.NorthwindConnectionString);

                    using (var context = new NorthwindContext(options))
                    {
                        Assert.Equal(91, await context.Customers.CountAsync());
                    }
                }
            }
Exemple #11
0
            public async Task Can_query_with_explicit_services_and_OnConfiguring()
            {
                using (await SqlServerTestDatabase.Northwind())
                {
                    var serviceCollection = new ServiceCollection();
                    serviceCollection.AddEntityFramework().AddSqlServer();
                    var serviceProvider = serviceCollection.BuildServiceProvider();

                    using (var context = new NorthwindContext(serviceProvider))
                    {
                        Assert.Equal(91, await context.Customers.CountAsync());
                    }
                }
            }
Exemple #12
0
 public async Task Throws_on_attempt_to_use_context_with_no_store()
 {
     using (await SqlServerTestDatabase.Northwind())
     {
         Assert.Equal(
             GetString("FormatNoDataStoreConfigured"),
             Assert.Throws <InvalidOperationException>(() =>
         {
             using (var context = new NorthwindContext())
             {
                 Assert.Equal(91, context.Customers.Count());
             }
         }).Message);
     }
 }
Exemple #13
0
            public async Task Can_register_context_with_DI_container_and_have_it_injected()
            {
                var serviceCollection = new ServiceCollection();

                serviceCollection.AddEntityFramework().AddSqlServer();
                var serviceProvider = serviceCollection
                                      .AddTransient <NorthwindContext>()
                                      .AddTransient <MyController>()
                                      .BuildServiceProvider();

                using (await SqlServerTestDatabase.Northwind())
                {
                    await serviceProvider.GetService <MyController>().TestAsync();
                }
            }
Exemple #14
0
        public async Task Tracking_entities_asynchronously_returns_tracked_entities_back()
        {
            using (var testDatabase = await SqlServerTestDatabase.Northwind())
            {
                using (var db = new NorthwindContext())
                {
                    var customer = await db.Customers.FirstOrDefaultAsync();

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

                    // if references are different this will throw
                    db.Customers.Remove(customer);
                }
            }
        }
Exemple #15
0
        public NorthwindQueryFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                  .AddEntityFramework()
                  .AddSqlServer()
                  .UseLoggerFactory(_loggingFactory)
                  .ServiceCollection
                  .BuildServiceProvider();

            _testDatabase = SqlServerTestDatabase.Northwind().Result;

            _options
                = new DbContextOptions()
                  .UseModel(SetTableNames(CreateModel()))
                  .UseSqlServer(_testDatabase.Connection.ConnectionString);
        }
Exemple #16
0
        public async Task Can_enumerate_entity_set()
        {
            using (await SqlServerTestDatabase.Northwind())
            {
                using (var db = new NorthwindContext())
                {
                    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 #17
0
            public async Task Can_register_configuration_with_DI_container_and_have_it_injected()
            {
                var options = new DbContextOptions().UseSqlServer(SqlServerTestDatabase.NorthwindConnectionString);

                var serviceCollection = new ServiceCollection();

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

                using (await SqlServerTestDatabase.Northwind())
                {
                    await serviceProvider.GetService <MyController>().TestAsync();
                }
            }
            public async Task Can_query_with_explicit_services_and_explicit_config()
            {
                using (await SqlServerTestDatabase.Northwind())
                {
                    var serviceCollection = new ServiceCollection();
                    serviceCollection
                    .AddEntityFramework()
                    .AddSqlServer()
                    .UseLoggerFactory <LoggerFactory>();
                    var serviceProvider = serviceCollection.BuildServiceProvider();

                    var options = new DbContextOptions().UseSqlServer(SqlServerTestDatabase.NorthwindConnectionString);

                    using (var context = new NorthwindContext(serviceProvider, options))
                    {
                        Assert.Equal(91, await context.Customers.CountAsync());
                    }
                }
            }
Exemple #19
0
            public async Task Throws_on_attempt_to_use_SQL_Server_without_providing_connection_string()
            {
                using (await SqlServerTestDatabase.Northwind())
                {
                    var serviceCollection = new ServiceCollection();
                    serviceCollection.AddEntityFramework().AddSqlServer();
                    var serviceProvider = serviceCollection.BuildServiceProvider();

                    Assert.Equal(
                        GetString("FormatNoDataStoreConfigured"),
                        Assert.Throws <DataStoreException>(() =>
                    {
                        using (var context = new NorthwindContext(serviceProvider))
                        {
                            Assert.Equal(91, context.Customers.Count());
                        }
                    }).InnerException.Message);
                }
            }
Exemple #20
0
            public async Task Throws_on_attempt_to_use_store_with_no_store_services()
            {
                using (await SqlServerTestDatabase.Northwind())
                {
                    var serviceCollection = new ServiceCollection();
                    serviceCollection.AddEntityFramework();
                    var serviceProvider = serviceCollection.BuildServiceProvider();

                    Assert.Equal(
                        GetString("FormatNoDataStoreService"),
                        Assert.Throws <InvalidOperationException>(() =>
                    {
                        using (var context = new NorthwindContext(serviceProvider))
                        {
                            Assert.Equal(91, context.Customers.Count());
                        }
                    }).Message);
                }
            }
Exemple #21
0
            private async Task NestedContextTest(Func <BlogContext> createBlogContext, Func <NorthwindContext> createNorthwindContext)
            {
                using (await SqlServerTestDatabase.Northwind())
                {
                    using (var context0 = createBlogContext())
                    {
                        Assert.Equal(0, context0.ChangeTracker.Entries().Count());
                        var blog0 = context0.Add(new Blog {
                            Id = 1, Name = "Giddyup"
                        });
                        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);
                            }
                        }
                    }
                }
            }
Exemple #22
0
        public async Task Can_run_linq_query_on_entity_set()
        {
            using (await SqlServerTestDatabase.Northwind())
            {
                using (var db = new NorthwindContext())
                {
                    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);
                }
            }
        }