public void Can_replace_connection_with_different_provider() { var intializer = new ReplaceConnectionInitializer(); Database.SetInitializer(intializer); using (var context = new ReplaceConnectionContext()) { using (var newConnection = new LazyInternalConnection( context, new DbConnectionInfo( "Data Source=NewReplaceConnectionContextDatabase.sdf", "System.Data.SqlServerCe.4.0"))) { context.InternalContext.OverrideConnection(newConnection); Assert.IsType <SqlCeConnection>(context.Database.Connection); Assert.Equal( "NewReplaceConnectionContextDatabase.sdf", context.Database.Connection.Database); context.Database.Initialize(force: true); Assert.Equal( "NewReplaceConnectionContextDatabase.sdf", intializer.DatabaseNameUsedDuringInitialization); } } }
private void Can_replace_connection_implementation( ReplaceConnectionContext context, LazyInternalConnection newConnection) { Database.Delete(newConnection.Connection); Database.Delete(typeof(ReplaceConnectionContext).DatabaseName()); context.InternalContext.OverrideConnection(newConnection); context.Entities.Add( new PersistEntity { Name = "Testing" }); context.SaveChanges(); Assert.Same(newConnection.Connection, context.Database.Connection); Assert.True(Database.Exists(newConnection.Connection)); Assert.False(Database.Exists(typeof(ReplaceConnectionContext).DatabaseName())); // By pass EF just to make sure everything targetted the correct database var cmd = newConnection.Connection.CreateCommand(); cmd.CommandText = "SELECT Count(*) FROM PersistEntities"; cmd.Connection.Open(); Assert.Equal(1, cmd.ExecuteScalar()); cmd.Connection.Close(); }
public void LazyInternalConnection_provider_name_calculated_when_connection_from_config() { using (var connection = new LazyInternalConnection("name=LazyConnectionTest")) { Assert.Equal("System.Data.SqlServerCe.4.0", connection.ProviderName); } }
public void LazyInternalConnection_provider_name_calculated_when_connection_by_convention() { using (var connection = new LazyInternalConnection("ConnectionName")) { Assert.Equal("System.Data.SqlClient", connection.ProviderName); } }
public void Can_replace_connection() { var intializer = new ReplaceConnectionInitializer(); Database.SetInitializer(intializer); using (var context = new ReplaceConnectionContext()) { using (var newConnection = new LazyInternalConnection( context, new DbConnectionInfo( @"Server=.\SQLEXPRESS;Database=NewReplaceConnectionContextDatabase;Trusted_Connection=True;", "System.Data.SqlClient"))) { context.InternalContext.OverrideConnection(newConnection); Assert.IsType <SqlConnection>(context.Database.Connection); Assert.Equal("NewReplaceConnectionContextDatabase", context.Database.Connection.Database); context.Database.Initialize(force: true); Assert.Equal( "NewReplaceConnectionContextDatabase", intializer.DatabaseNameUsedDuringInitialization); } } }
public void Disposed_LazyInternalConnection_can_be_reused() { var internalConnection = new LazyInternalConnection("NameForFactory"); try { var disposed1 = 0; var disposed2 = 0; var connection1 = internalConnection.Connection; connection1.Disposed += (_, __) => disposed1++; internalConnection.Dispose(); Assert.Equal(1, disposed1); Assert.Equal(0, disposed2); var connection2 = internalConnection.Connection; connection2.Disposed += (_, __) => disposed2++; internalConnection.Dispose(); Assert.Equal(1, disposed1); Assert.Equal(1, disposed2); } finally { internalConnection.Dispose(); } }
public void LazyInternalConnection_can_calculate_ConnectionHasModel_true_from_config_file_without_initializing() { using (var connection = new LazyInternalConnection("name=EntityConnectionString")) { Assert.True(connection.ConnectionHasModel); Assert.False(connection.IsInitialized); } }
public void LazyInternalConnection_can_calculate_ConnectionHasModel_true_from_DbConnectionInfo_without_initializing() { using (var connection = new LazyInternalConnection(new DbConnectionInfo("EntityConnectionString"))) { Assert.True(connection.ConnectionHasModel); Assert.False(connection.IsInitialized); } }
// <summary> // Performs the operation defined by the given delegate using the given lazy connection, ensuring // that the lazy connection is disposed after use. // </summary> // <param name="lazyConnection"> Information used to create a DbConnection. </param> // <param name="operation"> The operation to perform. </param> // <returns> The return value of the operation. </returns> private static bool PerformDatabaseOp( LazyInternalConnection lazyConnection, Func <ObjectContext, bool> operation) { using (lazyConnection) { return(PerformDatabaseOp(lazyConnection.Connection, operation)); } }
public void Default_connection_factory_is_used_to_create_connection_from_connection_string() { using (var internalConnection = new LazyInternalConnection("Database=HardCodedConnectionString")) { var connection = internalConnection.Connection; Assert.IsType <SqlConnection>(connection); } }
public void LazyInternalConnection_can_calculate_ConnectionHasModel_false_from_convention_without_initializing() { using (var connection = new LazyInternalConnection("MyName")) { Assert.False(connection.ConnectionHasModel); Assert.False(connection.IsInitialized); } }
public void LazyInternalConnection_throws_when_cant_find_connection_from_DbConnectionInfo_in_config_file() { using (var connection = new LazyInternalConnection(new DbConnectionInfo("YouWontFindMe"))) { Assert.Equal( Strings.DbConnectionInfo_ConnectionStringNotFound("YouWontFindMe"), Assert.Throws <InvalidOperationException>(() => { var temp = connection.Connection; }).Message); } }
public void LazyInternalConnection_ConnectionHasModel_without_initializing_throws_when_connection_not_in_config_DbConnectionInfo() { using (var connection = new LazyInternalConnection(new DbConnectionInfo("WontFindMe"))) { Assert.Equal( Strings.DbConnectionInfo_ConnectionStringNotFound("WontFindMe"), Assert.Throws <InvalidOperationException>(() => { var temp = connection.ConnectionHasModel; }).Message); } }
public void Nice_exception_is_thrown_if_connection_string_in_app_config_does_not_contain_provider_name() { using (var internalConnection = new LazyInternalConnection("ConnectionWithoutProviderName")) { Assert.Equal( Strings.DbContext_ProviderNameMissing("ConnectionWithoutProviderName"), Assert.Throws <InvalidOperationException>(() => { var _ = internalConnection.Connection; }).Message); } }
public void Connection_from_factory_is_cached() { using (var internalConnection = new LazyInternalConnection("NameForFactory")) { var connection = internalConnection.Connection; Assert.Same(connection, internalConnection.Connection); Assert.Same(connection, internalConnection.Connection); } }
public void LazyInternalConnection_can_calculate_ConnectionHasModel_true_from_connection_string_without_initializing() { var efString = ConfigurationManager.ConnectionStrings["EntityConnectionString"].ConnectionString; using (var connection = new LazyInternalConnection(efString)) { Assert.True(connection.ConnectionHasModel); Assert.False(connection.IsInitialized); } }
public void Connection_from_hard_coded_connection_string_is_cached() { using (var internalConnection = new LazyInternalConnection("Database=HardCodedConnectionString")) { var connection = internalConnection.Connection; Assert.Same(connection, internalConnection.Connection); Assert.Same(connection, internalConnection.Connection); } }
public void Connection_from_app_config_is_cached() { using (var internalConnection = new LazyInternalConnection("Couger35.Hubcap.FullNameDbContext")) { var connection = internalConnection.Connection; Assert.Same(connection, internalConnection.Connection); Assert.Same(connection, internalConnection.Connection); } }
public void LazyInternalConnection_can_create_connection_from_DbConnectionInfo_from_config_file() { using (var connection = new LazyInternalConnection(new DbConnectionInfo("LazyConnectionTest"))) { Assert.IsType <SqlCeConnection>(connection.Connection); Assert.Equal("ConnectionFromAppConfig.sdf", connection.Connection.Database); Assert.Equal("LazyConnectionTest", connection.ConnectionStringName); Assert.Equal(DbConnectionStringOrigin.DbContextInfo, connection.ConnectionStringOrigin); } }
public void LazyInternalConnection_can_calculate_ConnectionHasModel_false_after_initializing() { using (var connection = new LazyInternalConnection("name=LazyConnectionTest")) { var x = connection.Connection; Assert.True(connection.IsInitialized); Assert.False(connection.ConnectionHasModel); } }
public void LazyInternalConnection_can_calculate_ConnectionHasModel_true_after_initializing() { using (var connection = new LazyInternalConnection("name=EntityConnectionString")) { var x = connection.Connection; Assert.True(connection.IsInitialized); Assert.True(connection.ConnectionHasModel); } }
public void Using_bad_connection_string_throws() { var expectedException = GenerateException(() => new SqlConnection("Step1=YouNeedToAsk?").Open()); using (var internalConnection = new LazyInternalConnection("Step1=YouNeedToAsk?")) { Assert.Equal( expectedException.Message, Assert.Throws <ArgumentException>(() => { var _ = internalConnection.Connection; }).Message); } }
public void Full_name_of_context_is_found_in_app_config() { using (var internalConnection = new LazyInternalConnection("Couger35.Hubcap.FullNameDbContext")) { var connection = internalConnection.Connection; Assert.Equal("FullNameInAppConfig", connection.Database); Assert.IsType <SqlConnection>(connection); } }
public void Name_of_context_with_namespace_and_Context_stripped_is_not_found_in_app_config() { using (var internalConnection = new LazyInternalConnection("Couger35.Hubcap.ShortNameStrippedContext")) { var connection = internalConnection.Connection; Assert.Equal("Couger35.Hubcap.ShortNameStrippedContext", connection.Database); Assert.IsType <SqlConnection>(connection); } }
public void Default_IDbConnectionFactory_is_used_to_create_connection() { using (var internalConnection = new LazyInternalConnection("NameNotInAppConfig")) { var connection = internalConnection.Connection; Assert.Equal("NameNotInAppConfig", connection.Database); Assert.IsType <SqlConnection>(connection); } }
public void LazyInternalConnection_provider_name_calculated_when_connection_from_DbConnectionInfo() { using ( var connection = new LazyInternalConnection( new DbConnectionInfo("Data Source=ConnectionFromDbConnectionInfo.sdf", "System.Data.SqlServerCe.4.0"))) { Assert.Equal("System.Data.SqlServerCe.4.0", connection.ProviderName); } }
public void Explicit_name_is_used_without_stripping_DbContext() { using (var internalConnection = new LazyInternalConnection("Couger35.Hubcap.NameForFactoryDbContext")) { var connection = internalConnection.Connection; Assert.Equal("Couger35.Hubcap.NameForFactoryDbContext", connection.Database); Assert.IsType <SqlConnection>(connection); } }
public void By_convention_name_does_not_have_DbContext_stripped() { using (var internalConnection = new LazyInternalConnection("Couger35.Hubcap.NameForFactoryDbContext")) { var connection = internalConnection.Connection; Assert.Equal("Couger35.Hubcap.NameForFactoryDbContext", connection.Database); Assert.IsType <SqlConnection>(connection); } }
public void Connection_created_from_hard_coded_connection_string_is_disposed_after_use() { var disposed = false; using (var internalConnection = new LazyInternalConnection("Database=HardCodedConnectionString")) { var connection = internalConnection.Connection; connection.Disposed += (_, __) => disposed = true; } Assert.True(disposed); }
public void Connection_created_from_factory_is_disposed_after_use() { var disposed = false; using (var internalConnection = new LazyInternalConnection("NameForFactory")) { var connection = internalConnection.Connection; connection.Disposed += (_, __) => disposed = true; } Assert.True(disposed); }