/// <summary>
 /// Creates a new database on the database server for the model defined in the backing context, but only
 /// if a database with the same name does not already exist on the server.
 /// </summary>
 /// <returns> True if the database did not exist and was created; false otherwise. </returns>
 public bool CreateIfNotExists()
 {
   if (this._internalContext.DatabaseOperations.Exists(this._internalContext.Connection, this._internalContext.CommandTimeout, new Lazy<StoreItemCollection>(new Func<StoreItemCollection>(this.CreateStoreItemCollection))))
     return false;
   using (ClonedObjectContext contextForDdlOps = this._internalContext.CreateObjectContextForDdlOps())
     this._internalContext.CreateDatabase((ObjectContext) contextForDdlOps.ObjectContext, DatabaseExistenceState.DoesNotExist);
   return true;
 }
        public void ClonedObjectContext_ObjectContext_returns_the_cloned_ObjectContext()
        {
            var mockClonedContext = new Mock <ObjectContextProxy>();
            var mockContext       = CreateMockObjectContext(CreateMockConnection(), mockClonedContext);

            var clonedContext = new ClonedObjectContext(mockContext.Object, "Database=PinkyDinkyDo");

            mockContext.Verify(m => m.CreateNew(It.IsAny <EntityConnectionProxy>()));
            Assert.Same(mockClonedContext.Object, clonedContext.ObjectContext);
        }
        public void Cloning_an_ObjectContext_with_a_null_default_container_name_results_in_a_clone_without_a_default_container_name()
        {
            var mockClonedContext = new Mock <ObjectContextProxy>();
            var mockContext       = CreateMockObjectContext(CreateMockConnection(), mockClonedContext);

            var clonedContext = new ClonedObjectContext(mockContext.Object, "Database=PinkyDinkyDo");

            mockClonedContext.VerifySet(m => m.DefaultContainerName = It.IsAny <string>(), Times.Never());
            Assert.Null(clonedContext.ObjectContext.DefaultContainerName);
        }
 /// <summary>
 /// Deletes the database on the database server if it exists, otherwise does nothing.
 /// Calling this method from outside of an initializer will mark the database as having
 /// not been initialized. This means that if an attempt is made to use the database again
 /// after it has been deleted, then any initializer set will run again and, usually, will
 /// try to create the database again automatically.
 /// </summary>
 /// <returns> True if the database did exist and was deleted; false otherwise. </returns>
 public bool Delete()
 {
   if (!this._internalContext.DatabaseOperations.Exists(this._internalContext.Connection, this._internalContext.CommandTimeout, new Lazy<StoreItemCollection>(new Func<StoreItemCollection>(this.CreateStoreItemCollection))))
     return false;
   using (ClonedObjectContext contextForDdlOps = this._internalContext.CreateObjectContextForDdlOps())
   {
     this._internalContext.DatabaseOperations.Delete((ObjectContext) contextForDdlOps.ObjectContext);
     this._internalContext.MarkDatabaseNotInitialized();
   }
   return true;
 }
 internal void Create(DatabaseExistenceState existenceState)
 {
   if (existenceState == DatabaseExistenceState.Unknown)
   {
     if (this._internalContext.DatabaseOperations.Exists(this._internalContext.Connection, this._internalContext.CommandTimeout, new Lazy<StoreItemCollection>(new Func<StoreItemCollection>(this.CreateStoreItemCollection))))
       throw Error.Database_DatabaseAlreadyExists((object) DbInterception.Dispatch.Connection.GetDatabase(this._internalContext.Connection, new DbInterceptionContext().WithDbContext(this._internalContext.Owner)));
     existenceState = DatabaseExistenceState.DoesNotExist;
   }
   using (ClonedObjectContext contextForDdlOps = this._internalContext.CreateObjectContextForDdlOps())
     this._internalContext.CreateDatabase((ObjectContext) contextForDdlOps.ObjectContext, existenceState);
 }
        public void ClonedObjectContext_Connection_returns_the_cloned_store_connection()
        {
            var storeConnection = new SqlConnection();
            var mockContext     = CreateMockObjectContext(CreateMockConnection(storeConnection));

            var clonedConnection = new ClonedObjectContext(mockContext.Object, "Database=PinkyDinkyDo").Connection;

            Assert.NotSame(storeConnection, clonedConnection);
            Assert.Equal("Database=PinkyDinkyDo", clonedConnection.ConnectionString);
            Assert.Same(storeConnection.GetType(), clonedConnection.GetType());
        }
        public void Cloning_an_ObjectContext_with_a_default_container_name_copies_that_container_name()
        {
            var mockClonedContext = new Mock <ObjectContextProxy>();
            var mockContext       = CreateMockObjectContext(CreateMockConnection(), mockClonedContext);

            mockContext.Setup(m => m.DefaultContainerName).Returns("Kipper");

            var clonedContext = new ClonedObjectContext(mockContext.Object, "Database=PinkyDinkyDo");

            mockClonedContext.VerifySet(m => m.DefaultContainerName = It.IsAny <string>());
            Assert.Equal("Kipper", clonedContext.ObjectContext.DefaultContainerName);
        }
        public void Disposing_a_cloned_ObjectContext_disposes_both_the_context_and_the_connection()
        {
            var mockClonedContext = new Mock <ObjectContextProxy>();
            var mockContext       = CreateMockObjectContext(CreateMockConnection(), mockClonedContext);

            var clonedContext = new ClonedObjectContext(mockContext.Object, "Database=PinkyDinkyDo");

            var connectionIsDisposed = false;

            clonedContext.Connection.Disposed += (_, __) => connectionIsDisposed = true;

            clonedContext.Dispose();

            mockClonedContext.Verify(m => m.Dispose());
            Assert.True(connectionIsDisposed);
        }
        public void Calling_Dispose_on_an_already_disposed_cloned_ObjectContext_does_nothing()
        {
            var mockClonedContext = new Mock <ObjectContextProxy>();
            var mockContext       = CreateMockObjectContext(CreateMockConnection(), mockClonedContext);

            var clonedContext = new ClonedObjectContext(mockContext.Object, "Database=PinkyDinkyDo");

            var connectionIsDisposed = false;

            clonedContext.Connection.Disposed += (_, __) => connectionIsDisposed = true;

            clonedContext.Dispose();
            connectionIsDisposed = false;

            clonedContext.Dispose();

            mockClonedContext.Verify(m => m.Dispose(), Times.Once());
            Assert.False(connectionIsDisposed);
        }
 private StoreItemCollection CreateStoreItemCollection()
 {
   using (ClonedObjectContext contextForDdlOps = this._internalContext.CreateObjectContextForDdlOps())
     return (StoreItemCollection) (EntityConnection) contextForDdlOps.ObjectContext.Connection.GetMetadataWorkspace().GetItemCollection(DataSpace.SSpace);
 }