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); } } }
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); } } }
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 Exception_replacing_EntityConnection_with_DbConnection() { using (var context = new ReplaceConnectionContext("name=EntityConnectionString")) { using (var newConnection = new LazyInternalConnection("ByConventionName")) { Assert.Equal( Strings.LazyInternalContext_CannotReplaceEfConnectionWithDbConnection, Assert.Throws <InvalidOperationException>(() => context.InternalContext.OverrideConnection(newConnection)).Message); } } }
public void Exception_replacing_DbConnection_with_EntityConnection() { using (var context = new ReplaceConnectionContext()) { using (var newConnection = new EagerInternalConnection(context, new EntityConnection(), connectionOwned: true)) { Assert.Equal( Strings.LazyInternalContext_CannotReplaceDbConnectionWithEfConnection, Assert.Throws <InvalidOperationException>(() => context.InternalContext.OverrideConnection(newConnection)).Message); } } }
public void Can_replace_connection_with_different_provider() { using (var context = new ReplaceConnectionContext()) { using (var newConnection = new LazyInternalConnection( context, new DbConnectionInfo( "Data Source=NewReplaceConnectionContextDatabase.sdf", "System.Data.SqlServerCe.4.0"))) { Can_replace_connection_implementation(context, newConnection); } } }
public void Can_replace_connection() { using (var context = new ReplaceConnectionContext()) { using (var newConnection = new LazyInternalConnection( context, new DbConnectionInfo( SimpleConnectionString("NewReplaceConnectionContextDatabase"), "System.Data.SqlClient"))) { Can_replace_connection_implementation(context, newConnection); } } }