public void AddCollection() { new TestDefinition { TestName = "ChangeTracker_DbSetOperation_AddCollection_EF6", IterationCount = 100, WarmupCount = 5, Run = harness => { using (var context = new OrdersContext(_connectionString)) { var customers = new Customer[1000]; for (var i = 0; i < customers.Length; i++) { customers[i] = new Customer { Name = "Customer " + i }; } using (harness.StartCollection()) { context.Customers.AddRange(customers); } } } }.RunTest(); }
public void ValueFromObject() { new TestDefinition { TestName = "Query_Funcletization_ValueFromObject_EF6", IterationCount = 50, WarmupCount = 5, Setup = EnsureDatabaseSetup, Run = harness => { using (var context = new OrdersContext(_connectionString)) { using (harness.StartCollection()) { var valueHolder = new ValueHolder(); for (var i = 0; i < _funcletizationIterationCount; i++) { var result = context.Products.Where(p => p.ProductId < valueHolder.SecondLevelProperty).ToList(); Assert.Equal(10, result.Count); } } } } }.RunTest(); }
public void SameQueryInstance() { new TestDefinition { TestName = "Query_Funcletization_SameQueryInstance_EF6", IterationCount = 50, WarmupCount = 5, Setup = EnsureDatabaseSetup, Run = harness => { using (var context = new OrdersContext(_connectionString)) { using (harness.StartCollection()) { var val = 11; var query = context.Products.Where(p => p.ProductId < val); for (var i = 0; i < _funcletizationIterationCount; i++) { var result = query.ToList(); Assert.Equal(10, result.Count); } } } } }.RunTest(); }
private void InsertSeedData() { var products = CreateProducts(_productCount, setPrimaryKeys: false); using (var context = new OrdersContext(_connectionString)) { context.Products.AddRange(products); context.SaveChanges(); } var customers = CreateCustomers(_customerCount, setPrimaryKeys: false); using (var context = new OrdersContext(_connectionString)) { context.Customers.AddRange(customers); context.SaveChanges(); } var orders = CreateOrders(customers, _ordersPerCustomer, setPrimaryKeys: false); using (var context = new OrdersContext(_connectionString)) { context.Orders.AddRange(orders); context.SaveChanges(); } var lines = CreateOrderLines(products, orders, _linesPerOrder, setPrimaryKeys: false); using (var context = new OrdersContext(_connectionString)) { context.OrderLines.AddRange(lines); context.SaveChanges(); } }
public void AttachChildren() { new TestDefinition { TestName = "ChangeTracker_Fixup_AttachChildren_EF6", IterationCount = 10, WarmupCount = 5, Setup = EnsureDatabaseSetup, Run = harness => { List<Order> orders; using (var context = new OrdersContext(_connectionString)) { orders = context.Orders.ToList(); } using (var context = new OrdersContext(_connectionString)) { var customers = context.Customers.ToList(); Assert.Equal(1000, orders.Count); Assert.Equal(1000, customers.Count); foreach (var order in orders) { using (harness.StartCollection()) { context.Orders.Attach(order); } Assert.Same(order, order.Customer.Orders.Single()); } } } }.RunTest(); }
private bool IsDatabaseCorrect(OrdersContext context) { return context.Database.CompatibleWithModel(throwIfNoMetadata: true) && _productCount == context.Products.Count() && _customerCount == context.Customers.Count() && _customerCount * _ordersPerCustomer == context.Orders.Count() && _customerCount * _ordersPerCustomer * _linesPerOrder == context.OrderLines.Count(); }
private static void Insert(TestHarness harness) { using (var context = new OrdersContext(_connectionString)) { using (context.Database.BeginTransaction()) { for (var i = 0; i < 1000; i++) { context.Customers.Add(new Customer { Name = "New Customer " + i }); } harness.StartCollection(); var records = context.SaveChanges(); harness.StopCollection(); Assert.Equal(1000, records); } } }
private void EnsureDatabaseCreated() { using (var context = new OrdersContext(_connectionString)) { if (!context.Database.Exists()) { context.Database.Create(); InsertSeedData(); } else if (!IsDatabaseCorrect(context)) { context.Database.Delete(); context.Database.Create(); InsertSeedData(); } Assert.True(IsDatabaseCorrect(context)); } }
private static void Update(TestHarness harness) { using (var context = new OrdersContext(_connectionString)) { using (context.Database.BeginTransaction()) { foreach (var customer in context.Customers) { customer.Name += " Modified"; } harness.StartCollection(); var records = context.SaveChanges(); harness.StopCollection(); Assert.Equal(1000, records); } } }
public void Add(TestHarness harness, bool autoDetectChanges) { using (var context = new OrdersContext(_connectionString)) { var customers = new Customer[1000]; for (var i = 0; i < customers.Length; i++) { customers[i] = new Customer { Name = "Customer " + i }; } context.Configuration.AutoDetectChangesEnabled = autoDetectChanges; using (harness.StartCollection()) { foreach (var customer in customers) { context.Customers.Add(customer); } } } }
public void OrderBy() { new TestDefinition { TestName = "Query_Simple_OrderBy_EF6", IterationCount = 100, WarmupCount = 5, Setup = EnsureDatabaseSetup, Run = harness => { using (var context = new OrdersContext(_connectionString)) { harness.StartCollection(); var result = context.Products.OrderBy(p => p.Retail).ToList(); harness.StopCollection(); Assert.Equal(1000, result.Count); } } }.RunTest(); }
private void EnsureDatabaseCreated() { using (var context = new OrdersContext(_connectionString)) { if (!context.Database.Exists()) { context.Database.Create(); InsertSeedData(); OnDatabaseCreated(context); } else if (!IsDatabaseCorrect(context)) { context.Database.Delete(); context.Database.Create(); InsertSeedData(); OnDatabaseCreated(context); } Assert.True(IsDatabaseCorrect(context)); } }
public void EnsureCreated( string connectionString, int productCount, int customerCount, int ordersPerCustomer, int linesPerOrder) { using (var context = new OrdersContext(connectionString)) { if (!context.Database.Exists()) { context.Database.Create(); InsertSeedData(connectionString, productCount, customerCount, ordersPerCustomer, linesPerOrder); } Assert.Equal(productCount, context.Products.Count()); Assert.Equal(customerCount, context.Customers.Count()); Assert.Equal(customerCount * ordersPerCustomer, context.Orders.Count()); Assert.Equal(customerCount * ordersPerCustomer * linesPerOrder, context.OrderLines.Count()); } }
public void InsertSeedData( string connectionString, int productCount, int customerCount, int ordersPerCustomer, int linesPerOrder) { List <Product> products = CreateProducts(productCount); using (var context = new OrdersContext(connectionString)) { context.Products.AddRange(products); context.SaveChanges(); } List <Customer> customers = CreateCustomers(customerCount); using (var context = new OrdersContext(connectionString)) { context.Customers.AddRange(customers); context.SaveChanges(); } List <Order> orders = CreateOrders(ordersPerCustomer, customers); using (var context = new OrdersContext(connectionString)) { context.Orders.AddRange(orders); context.SaveChanges(); } List <OrderLine> lines = CreateOrderLines(linesPerOrder, products, orders); using (var context = new OrdersContext(connectionString)) { context.OrderLines.AddRange(lines); context.SaveChanges(); } }
public void InsertSeedData( string connectionString, int productCount, int customerCount, int ordersPerCustomer, int linesPerOrder) { var products = CreateProducts(productCount); using (var context = new OrdersContext(connectionString)) { context.Products.AddRange(products); context.SaveChanges(); } var customers = CreateCustomers(customerCount); using (var context = new OrdersContext(connectionString)) { context.Customers.AddRange(customers); context.SaveChanges(); } var orders = CreateOrders(ordersPerCustomer, customers); using (var context = new OrdersContext(connectionString)) { context.Orders.AddRange(orders); context.SaveChanges(); } var lines = CreateOrderLines(linesPerOrder, products, orders); using (var context = new OrdersContext(connectionString)) { context.OrderLines.AddRange(lines); context.SaveChanges(); } }
public void GroupBy() { new TestDefinition { TestName = "Query_Simple_GroupBy_EF6", IterationCount = 100, WarmupCount = 5, Setup = EnsureDatabaseSetup, Run = harness => { using (var context = new OrdersContext(_connectionString)) { harness.StartCollection(); var result = context.Products .GroupBy(p => p.Retail) .Select(g => new { Retail = g.Key, Products = g }) .ToList(); harness.StopCollection(); Assert.Equal(10, result.Count); Assert.All(result, g => Assert.Equal(100, g.Products.Count())); } } }.RunTest(); }
public void Remove(TestHarness harness, bool autoDetectChanges) { using (var context = new OrdersContext(_connectionString)) { var customers = context.Customers.ToArray(); Assert.Equal(1000, customers.Length); context.Configuration.AutoDetectChangesEnabled = autoDetectChanges; using (harness.StartCollection()) { foreach (var customer in customers) { context.Customers.Remove(customer); } } } }
public void RemoveCollection() { new TestDefinition { TestName = "ChangeTracker_DbSetOperation_RemoveCollection_EF6", IterationCount = 100, WarmupCount = 5, Setup = EnsureDatabaseSetup, Run = harness => { using (var context = new OrdersContext(_connectionString)) { var customers = context.Customers.ToArray(); Assert.Equal(1000, customers.Length); using (harness.StartCollection()) { context.Customers.RemoveRange(customers); } } } }.RunTest(); }
public void Update(TestHarness harness, bool autoDetectChanges) { using (var context = new OrdersContext(_connectionString)) { var customers = GetAllCustomersFromDatabase(); Assert.Equal(1000, customers.Length); context.Configuration.AutoDetectChangesEnabled = autoDetectChanges; using (harness.StartCollection()) { foreach (var customer in customers) { context.Entry(customer).State = EntityState.Modified; } } } }
// Note: UpdateCollection() not implemented because there is no // API for bulk update in EF6.x private static Customer[] GetAllCustomersFromDatabase() { using (var context = new OrdersContext(_connectionString)) { return context.Customers.ToArray(); } }
public void QueryParents() { new TestDefinition { TestName = "ChangeTracker_Fixup_QueryParents_EF6", IterationCount = 10, WarmupCount = 5, Setup = EnsureDatabaseSetup, Run = harness => { using (var context = new OrdersContext(_connectionString)) { context.Orders.ToList(); harness.StartCollection(); var customers = context.Customers.ToList(); harness.StopCollection(); Assert.Equal(1000, context.ChangeTracker.Entries<Customer>().Count()); Assert.Equal(1000, context.ChangeTracker.Entries<Order>().Count()); Assert.All(customers, c => Assert.Equal(1, c.Orders.Count)); } } }.RunTest(); }
protected virtual void OnDatabaseCreated(OrdersContext context) { }
public void ProjectionAcrossNavigation() { new TestDefinition { TestName = "Query_Simple_ProjectionAcrossNavigation_EF6", IterationCount = 100, WarmupCount = 5, Setup = EnsureDatabaseSetup, Run = harness => { using (var context = new OrdersContext(_connectionString)) { harness.StartCollection(); var result = context.Orders.Select(o => new { CustomerName = o.Customer.Name, OrderDate = o.Date }) .ToList(); harness.StopCollection(); Assert.Equal(2000, result.Count); } } }.RunTest(); }
public void Include() { new TestDefinition { TestName = "Query_Simple_Include_EF6", IterationCount = 100, WarmupCount = 5, Setup = EnsureDatabaseSetup, Run = harness => { using (var context = new OrdersContext(_connectionString)) { harness.StartCollection(); var result = context.Customers.Include(c => c.Orders).ToList(); harness.StopCollection(); Assert.Equal(1000, result.Count); Assert.Equal(2000, result.SelectMany(c => c.Orders).Count()); } } }.RunTest(); }
private static void Mixed(TestHarness harness) { using (var context = new OrdersContext(_connectionString)) { using (context.Database.BeginTransaction()) { var customers = context.Customers.ToArray(); for (var i = 0; i < 333; i++) { context.Customers.Add(new Customer { Name = "New Customer " + i }); } for (var i = 0; i < 1000; i += 3) { context.Customers.Remove(customers[i]); } for (var i = 1; i < 1000; i += 3) { customers[i].Name += " Modified"; } harness.StartCollection(); var records = context.SaveChanges(); harness.StopCollection(); Assert.Equal(1000, records); } } }