public DatabaseTests() { using (var context = new SimpleModelContext()) { context.Database.Initialize(force: false); } using (var context = new SimpleModelContextWithNoData()) { context.Database.Initialize(force: false); } using (var connection = new SqlConnection(SimpleConnectionString("master"))) { connection.Open(); using (var command = connection.CreateCommand()) { command.CommandText = string.Format( @"IF NOT EXISTS (SELECT * FROM sys.server_principals WHERE name = N'EFTestSimpleModelUser') BEGIN CREATE LOGIN [EFTestSimpleModelUser] WITH PASSWORD=N'Password1', DEFAULT_DATABASE=[{0}], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF DENY VIEW ANY DATABASE TO [EFTestSimpleModelUser] CREATE USER [EFTestSimpleModelUser] FOR LOGIN [EFTestSimpleModelUser] DENY SELECT TO [EFTestSimpleModelUser] GRANT CREATE DATABASE TO [EFTestSimpleModelUser] END", DefaultDbName <SimpleModelContext>()); command.ExecuteNonQuery(); } } }
public DatabaseTests() { using (var context = new SimpleModelContext()) { context.Database.Initialize(force: false); } using (var context = new SimpleModelContextWithNoData()) { context.Database.Initialize(force: false); } using (var connection = new SqlConnection(SimpleConnectionString("master"))) { connection.Open(); if (DatabaseTestHelpers.IsSqlAzure(connection.ConnectionString)) { CreateLoginForSqlAzure(connection); } else { CreateLoginForSqlServer(connection); } } }
public void DbContext_construction_does_not_throw_but_subsequent_calls_using_connection_throw_for_invalid_SqlCE_connection_string() { var sqlCeAssembly = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0").CreateConnection("Dummy").GetType().Assembly; var context = new SimpleModelContextWithNoData("Data Sourc=Scenario_Use_AppConfig.sdf"); Assert.Throws <ArgumentException>(() => GetObjectContext(context)).ValidateMessage( sqlCeAssembly, "ADP_KeywordNotSupported", "System.Data.SqlServerCe", "data sourc"); }
public void Sets_are_initialized_but_do_not_change_model_using_name_and_model_constructor_on_DbContext_on_SqlCe() { var model = new DbModelBuilder().Build(ProviderRegistry.SqlCe4_ProviderInfo).Compile(); using (var context = new SimpleModelContextWithNoData(DefaultDbName <EmptyContext>(), model)) { Assert.NotNull(context.Products); Assert.NotNull(context.Categories); context.Assert <Product>().IsNotInModel(); context.Assert <Category>().IsNotInModel(); } }
private void Verify_DbContext_construction_using_connection_string_ctor( string nameOrConnectionString, string expectedDatabaseName) { using (var context = new SimpleModelContextWithNoData(nameOrConnectionString)) { Assert.NotNull(context.Products); Assert.NotNull(context.Categories); context.Assert <Product>().IsInModel(); context.Assert <Category>().IsInModel(); Assert.Equal(expectedDatabaseName, context.Database.Connection.Database); } }
private void InsertIntoCleanContext(SimpleModelContextWithNoData context) { context.Categories.Add( new Category { Id = "Large Hadron Collider" }); context.Products.Add( new Product { Name = "Higgs Boson", CategoryId = "Large Hadron Collider" }); context.SaveChanges(); }
public void Scenario_Use_AppConfig_connection_string() { Database.Delete("Scenario_Use_AppConfig_connection_string"); using (var context = new SimpleModelContextWithNoData("Scenario_Use_AppConfig_connection_string")) { Assert.Equal("Scenario_Use_AppConfig", context.Database.Connection.Database); InsertIntoCleanContext(context); } using (var context = new SimpleModelContextWithNoData("Scenario_Use_AppConfig_connection_string")) { ValidateFromCleanContext(context); } }
private void ValidateFromCleanContext(SimpleModelContextWithNoData 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)); }
public void Scenario_Use_AppConfig_connection_string_OnSqlCe() { const string connectionStringKey = "Scenario_Use_SqlCeLegacy_AppConfig_connection_string"; Database.Delete(connectionStringKey); using (var context = new SimpleModelContextWithNoData(connectionStringKey)) { Assert.Equal("Scenario_Use_AppConfig_Legacy.sdf", context.Database.Connection.Database); InsertIntoCleanContext(context); } using (var context = new SimpleModelContextWithNoData(connectionStringKey)) { ValidateFromCleanContext(context); } }
public void Scenario_CodeFirst_with_ModelBuilder_OnSqlCe() { Database.Delete("Scenario_CodeFirstWithModelBuilder"); var builder = new DbModelBuilder(); builder.Entity <Product>(); builder.Entity <Category>(); var model = builder.Build(ProviderRegistry.SqlCe35_ProviderInfo).Compile(); using (var context = new SimpleModelContextWithNoData("Scenario_CodeFirstWithModelBuilder", model)) { InsertIntoCleanContext(context); } using (var context = new SimpleModelContextWithNoData("Scenario_CodeFirstWithModelBuilder", model)) { ValidateFromCleanContext(context); } }
public void Dispose_disposes_underlying_ObjectContext_if_DbContext_owns_it_when_constructed_using_DbCompiledModel_constructor() { // Arrange var builder = new DbModelBuilder(); ObjectContext objectContext = null; DbConnection storeConnection = null; using ( var context = new SimpleModelContextWithNoData(builder.Build(ProviderRegistry.Sql2008_ProviderInfo).Compile())) { objectContext = GetObjectContext(context); storeConnection = context.Database.Connection; } // Assert Assert.Throws <ObjectDisposedException>(() => objectContext.SaveChanges()).ValidateMessage( "ObjectContext_ObjectDisposed"); Assert.True( storeConnection.State == ConnectionState.Closed && storeConnection.ConnectionString.Equals(string.Empty)); }
private void Verify_DbContext_construction_using_connection_and_model_Ctor( DbConnection connection, DbCompiledModelContents contents) { // Arrange // DbCompiledModel creation as appropriate for the various model content options var builder = new DbModelBuilder(); switch (contents) { 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, " + contents); } var model = builder.Build(connection).Compile(); // Act using (var context = new SimpleModelContextWithNoData(connection, model)) { // Verification as appropriate for the various model content options switch (contents) { 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, " + contents); } } }
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); } } }