public void Scenario_Use_AppConfig_connection_string() { Database.Delete("Scenario_Use_AppConfig_LocalDb_connection_string"); using (var context = new SimpleLocalDbModelContextWithNoData("Scenario_Use_AppConfig_LocalDb_connection_string")) { Assert.Equal("Scenario_Use_AppConfig_LocalDb", context.Database.Connection.Database); InsertIntoCleanContext(context); } using (var context = new SimpleLocalDbModelContextWithNoData("Scenario_Use_AppConfig_LocalDb_connection_string")) { ValidateFromCleanContext(context); } }
private void ValidateFromCleanContext(SimpleLocalDbModelContextWithNoData context) { var product = context.Products.Find(1); var category = context.Categories.Find("Large Hadron Collider"); // Scenario ends; simple validation of final state follows Assert.NotNull(product); Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State); Assert.NotNull(category); Assert.Equal(EntityState.Unchanged, GetStateEntry(context, category).State); Assert.Equal("Large Hadron Collider", product.CategoryId); Assert.Same(category, product.Category); Assert.True(category.Products.Contains(product)); Assert.Equal(@"(localdb)\v11.0", context.Database.Connection.DataSource); }
private void InsertIntoCleanContext(SimpleLocalDbModelContextWithNoData context) { context.Categories.Add( new Category { Id = "Large Hadron Collider" }); context.Products.Add( new Product { Name = "Higgs Boson", CategoryId = "Large Hadron Collider" }); context.SaveChanges(); Assert.Equal(@"(localdb)\v11.0", context.Database.Connection.DataSource); }
public void Scenario_CodeFirst_with_ModelBuilder() { Database.Delete("Scenario_CodeFirstWithModelBuilder"); var builder = new DbModelBuilder(); builder.Entity<Product>(); builder.Entity<Category>(); var model = builder.Build(ProviderRegistry.Sql2008_ProviderInfo).Compile(); using (var context = new SimpleLocalDbModelContextWithNoData("Scenario_CodeFirstWithModelBuilder", model)) { InsertIntoCleanContext(context); } using (var context = new SimpleLocalDbModelContextWithNoData("Scenario_CodeFirstWithModelBuilder", model)) { ValidateFromCleanContext(context); } }