private void DbContext_should_dispose_underlying_context_and_connection_if_it_does_not_own_it( ConnectionStringFormat connStringFormat, DbCompiledModel model = null) { // Arrange string connectionString = null; switch (connStringFormat) { case ConnectionStringFormat.DatabaseName: connectionString = DefaultDbName <SimpleModelContextWithNoData>(); break; case ConnectionStringFormat.NamedConnectionString: connectionString = "SimpleModelWithNoDataFromAppConfig"; break; case ConnectionStringFormat.ProviderConnectionString: connectionString = SimpleConnectionString <SimpleModelContextWithNoData>(); break; default: throw new ArgumentException( "Invalid Connection String Format specified" + connStringFormat); } ObjectContext objectContext = null; DbConnection connection = null; // Act using ( var context = model == null ? new SimpleModelContextWithNoData(connectionString) : new SimpleModelContextWithNoData(connectionString, model)) { objectContext = GetObjectContext(context); connection = context.Database.Connection; } // Assert Assert.Throws <ObjectDisposedException>(() => objectContext.SaveChanges()).ValidateMessage( "ObjectContext_ObjectDisposed"); Assert.True(connection.State == ConnectionState.Closed && connection.ConnectionString.Equals(string.Empty)); }
private void DbContext_construction_using_connection_string_and_model_Ctor( ConnectionStringFormat connStringFormat, DbCompiledModelContents modelContents) { // Act // Setup connection string string connectionString = null; string dbName = null; switch (connStringFormat) { case ConnectionStringFormat.DatabaseName: connectionString = dbName = DefaultDbName<SimpleModelContextWithNoData>(); break; case ConnectionStringFormat.NamedConnectionString: connectionString = "SimpleModelWithNoDataFromAppConfig"; dbName = "SimpleModel.SimpleModelWithNoData"; break; case ConnectionStringFormat.ProviderConnectionString: connectionString = SimpleConnectionString<SimpleModelContextWithNoData>(); dbName = "SimpleModel.SimpleModelContextWithNoData"; break; default: throw new ArgumentException("Invalid ConnectionStringFormat enum supplied " + connStringFormat); } // Setup DbCompiledModel var builder = new DbModelBuilder(); switch (modelContents) { case DbCompiledModelContents.IsEmpty: // Do nothing as builder has already been initialized break; case DbCompiledModelContents.IsSubset: // Product is not defined here builder.Entity<Category>(); break; case DbCompiledModelContents.IsSuperset: builder.Entity<Category>(); builder.Entity<Product>(); builder.Entity<Login>(); break; case DbCompiledModelContents.Match: builder.Entity<Category>(); builder.Entity<Product>(); break; case DbCompiledModelContents.DontMatch: builder.Entity<FeaturedProduct>(); builder.Entity<Login>(); break; default: throw new ArgumentException("Invalid DbCompiledModelContents Arguments passed in, " + modelContents); } // Act using ( var context = new SimpleModelContextWithNoData(connectionString, builder.Build(ProviderRegistry.Sql2008_ProviderInfo). Compile())) { // Assert Assert.Equal(context.Database.Connection.Database, dbName); switch (modelContents) { case DbCompiledModelContents.IsEmpty: Assert.NotNull(context.Categories); Assert.NotNull(context.Categories); context.Assert<Category>().IsNotInModel(); context.Assert<Product>().IsNotInModel(); break; case DbCompiledModelContents.IsSubset: Assert.NotNull(context.Categories); Assert.NotNull(context.Categories); context.Assert<Category>().IsInModel(); context.Assert<Product>().IsInModel(); // reachability break; case DbCompiledModelContents.IsSuperset: Assert.NotNull(context.Categories); Assert.NotNull(context.Categories); context.Assert<Category>().IsInModel(); context.Assert<Product>().IsInModel(); context.Assert<Login>().IsInModel(); break; case DbCompiledModelContents.Match: Assert.NotNull(context.Categories); Assert.NotNull(context.Categories); context.Assert<Category>().IsInModel(); context.Assert<Product>().IsInModel(); context.Assert<Login>().IsNotInModel(); break; case DbCompiledModelContents.DontMatch: Assert.NotNull(context.Categories); Assert.NotNull(context.Products); context.Assert<Login>().IsInModel(); context.Assert<FeaturedProduct>().IsInModel(); break; default: throw new ArgumentException("Invalid DbCompiledModelContents Arguments passed in, " + modelContents); } } }
private void DbContext_construction_using_connection_string_and_model_Ctor( ConnectionStringFormat connStringFormat, DbCompiledModelContents modelContents) { // Act // Setup connection string string connectionString = null; string dbName = null; switch (connStringFormat) { case ConnectionStringFormat.DatabaseName: connectionString = dbName = DefaultDbName <SimpleModelContextWithNoData>(); break; case ConnectionStringFormat.NamedConnectionString: connectionString = "Scenario_Use_SqlCe_AppConfig_connection_string"; break; case ConnectionStringFormat.ProviderConnectionString: connectionString = SimpleCeConnectionString <SimpleModelContextWithNoData>(); break; default: throw new ArgumentException("Invalid ConnectionStringFormat enum supplied " + connStringFormat); } // Setup DbCompiledModel var builder = new DbModelBuilder(); switch (modelContents) { case DbCompiledModelContents.IsEmpty: // Do nothing as builder has already been initialized break; case DbCompiledModelContents.IsSubset: // Product is not defined here builder.Entity <Category>(); break; case DbCompiledModelContents.IsSuperset: builder.Entity <Category>(); builder.Entity <Product>(); builder.Entity <Login>(); break; case DbCompiledModelContents.Match: builder.Entity <Category>(); builder.Entity <Product>(); break; case DbCompiledModelContents.DontMatch: builder.Entity <FeaturedProduct>(); builder.Entity <Login>(); break; default: throw new ArgumentException("Invalid DbCompiledModelContents Arguments passed in, " + modelContents); } // Act using ( var context = new SimpleModelContextWithNoData( connectionString, builder.Build(ProviderRegistry.SqlCe4_ProviderInfo). Compile())) { // Assert switch (modelContents) { case DbCompiledModelContents.IsEmpty: Assert.NotNull(context.Categories); Assert.NotNull(context.Categories); context.Assert <Category>().IsNotInModel(); context.Assert <Product>().IsNotInModel(); break; case DbCompiledModelContents.IsSubset: Assert.NotNull(context.Categories); Assert.NotNull(context.Categories); context.Assert <Category>().IsInModel(); context.Assert <Product>().IsInModel(); // Reachability break; case DbCompiledModelContents.IsSuperset: Assert.NotNull(context.Categories); Assert.NotNull(context.Categories); context.Assert <Category>().IsInModel(); context.Assert <Product>().IsInModel(); context.Assert <Login>().IsInModel(); break; case DbCompiledModelContents.Match: Assert.NotNull(context.Categories); Assert.NotNull(context.Categories); context.Assert <Category>().IsInModel(); context.Assert <Product>().IsInModel(); context.Assert <Login>().IsNotInModel(); break; case DbCompiledModelContents.DontMatch: Assert.NotNull(context.Categories); Assert.NotNull(context.Products); context.Assert <Login>().IsInModel(); context.Assert <FeaturedProduct>().IsInModel(); break; default: throw new ArgumentException( "Invalid DbCompiledModelContents Arguments passed in, " + modelContents); } } }