/// <summary>
		/// Saves the given diagram to the given file, with default encoding (UTF-8).
		/// </summary>
		/// <param name="serializationResult">Stores serialization result from the save operation.</param>
		/// <param name="modelRoot">nHydrateModel instance to be saved.</param>
		/// <param name="modelFileName">Name of the file in which the CanonicalSampleRoot instance will be saved.</param>
		/// <param name="diagram">nHydrateDiagram to be saved.</param>
		/// <param name="diagramFileName">Name of the file in which the diagram will be saved.</param>
		/// <param name="writeOptionalPropertiesWithDefaultValue">Whether optional properties with default value will be saved.</param>
		public virtual void SaveModelAndDiagram(DslModeling::SerializationResult serializationResult, nHydrateModel modelRoot, string modelFileName, nHydrateDiagram diagram, string diagramFileName, bool writeOptionalPropertiesWithDefaultValue)
		{
			this.SaveModelAndDiagram(serializationResult, modelRoot, modelFileName, diagram, diagramFileName, global::System.Text.Encoding.UTF8, writeOptionalPropertiesWithDefaultValue);
		}
		public virtual void SaveModelAndDiagram(DslModeling::SerializationResult serializationResult, nHydrateModel modelRoot, string modelFileName, nHydrateDiagram diagram, string diagramFileName, global::System.Text.Encoding encoding, bool writeOptionalPropertiesWithDefaultValue)
		{
			#region Check Parameters
			if (serializationResult == null)
				throw new global::System.ArgumentNullException("serializationResult");
			if (string.IsNullOrEmpty(modelFileName))
				throw new global::System.ArgumentNullException("modelFileName");
			if (diagram == null)
				throw new global::System.ArgumentNullException("diagram");
			if (string.IsNullOrEmpty(diagramFileName))
				throw new global::System.ArgumentNullException("diagramFileName");
			#endregion
	
			if (serializationResult.Failed)
				return;
	
			// Save the model file first
			using (global::System.IO.MemoryStream modelFileContent = this.InternalSaveModel(serializationResult, modelRoot, modelFileName, encoding, writeOptionalPropertiesWithDefaultValue))
			{
				if (serializationResult.Failed)
					return;
	
				using (global::System.IO.MemoryStream diagramFileContent = this.InternalSaveDiagram(serializationResult, diagram, diagramFileName, encoding, writeOptionalPropertiesWithDefaultValue))
				{
					if (!serializationResult.Failed)
					{
						// Only write the contents if there's no error encountered during serialization.
						if (modelFileContent != null)
						{
							using (global::System.IO.FileStream fileStream = new global::System.IO.FileStream(modelFileName, global::System.IO.FileMode.Create, global::System.IO.FileAccess.Write, global::System.IO.FileShare.None))
							{
								using (global::System.IO.BinaryWriter writer = new global::System.IO.BinaryWriter(fileStream, encoding))
								{
									writer.Write(modelFileContent.ToArray());
								}
							}
						}
						if (diagramFileContent != null)
						{
							using (global::System.IO.FileStream fileStream = new global::System.IO.FileStream(diagramFileName, global::System.IO.FileMode.Create, global::System.IO.FileAccess.Write, global::System.IO.FileShare.None))
							{
								using (global::System.IO.BinaryWriter writer = new global::System.IO.BinaryWriter(fileStream, encoding))
								{
									writer.Write(diagramFileContent.ToArray());
								}
							}
						}
					}
				}
			}
		}
		/// <summary>
		/// Saves the given diagram to the given file, with default encoding (UTF-8), and optional properties with default value will not
		/// be written out.
		/// </summary>
		/// <param name="serializationResult">Stores serialization result from the save operation.</param>
		/// <param name="modelRoot">nHydrateModel instance to be saved.</param>
		/// <param name="modelFileName">Name of the file in which the CanonicalSampleRoot instance will be saved.</param>
		/// <param name="diagram">nHydrateDiagram to be saved.</param>
		/// <param name="diagramFileName">Name of the file in which the diagram will be saved.</param>
		public virtual void SaveModelAndDiagram(DslModeling::SerializationResult serializationResult, nHydrateModel modelRoot, string modelFileName, nHydrateDiagram diagram, string diagramFileName)
		{
			this.SaveModelAndDiagram(serializationResult, modelRoot, modelFileName, diagram, diagramFileName, global::System.Text.Encoding.UTF8, false);
		}
		internal protected virtual nHydrateDiagram CreateDiagramHelper(DslModeling::Partition diagramPartition, DslModeling::ModelElement modelRoot)
		{
			nHydrateDiagram diagram = new nHydrateDiagram(diagramPartition);
			return diagram;
		}
		internal global::System.IO.MemoryStream InternalSaveDiagram(DslModeling::SerializationResult serializationResult, nHydrateDiagram diagram, string diagramFileName, global::System.Text.Encoding encoding, bool writeOptionalPropertiesWithDefaultValue)
		{
			#region Check Parameters
			global::System.Diagnostics.Debug.Assert(serializationResult != null);
			global::System.Diagnostics.Debug.Assert(diagram != null);
			global::System.Diagnostics.Debug.Assert(!serializationResult.Failed);
			#endregion
	
			DslModeling::DomainXmlSerializerDirectory directory = this.GetDirectory(diagram.Store);
	
			
			global::System.IO.MemoryStream newFileContent = new global::System.IO.MemoryStream();
			
			DslModeling::SerializationContext serializationContext = new DslModeling::SerializationContext(directory, diagramFileName, serializationResult);
			this.InitializeSerializationContext(diagram.Partition, serializationContext, false);
			// MonikerResolver shouldn't be required in Save operation, so not calling SetupMonikerResolver() here.
			serializationContext.WriteOptionalPropertiesWithDefaultValue = writeOptionalPropertiesWithDefaultValue;
			global::System.Xml.XmlWriterSettings settings = nHydrate2SerializationHelper.Instance.CreateXmlWriterSettings(serializationContext, true, encoding);
			using (global::System.Xml.XmlWriter writer = global::System.Xml.XmlWriter.Create(newFileContent, settings))
			{
				this.WriteRootElement(serializationContext, diagram, writer);
			}
	
			return newFileContent;
		}
 /// <summary>
 /// Customize Model and Diagram Loading.
 /// </summary>
 /// <param name="serializationResult">Stores serialization result from the load operation.</param>
 /// <param name="modelPartition">Partition in which the new DslLibrary instance will be created.</param>
 /// <param name="modelFileName">Name of the file from which the DslLibrary instance will be deserialized.</param>
 /// <param name="diagramPartition">Partition in which the new DslDesignerDiagram instance will be created.</param>
 /// <param name="diagramFileName">Name of the file from which the DslDesignerDiagram instance will be deserialized.</param>
 /// <param name="modelRoot">The root of the file that was loaded.</param>
 /// <param name="diagram">The diagram matching the modelRoot.</param>
 private void OnPostLoadModelAndDiagram(DslModeling::SerializationResult serializationResult, DslModeling::Partition modelPartition, string modelFileName, DslModeling::Partition diagramPartition, string diagramFileName, nHydrateModel modelRoot, nHydrateDiagram diagram)
 {
 }
        public override void SaveModelAndDiagram(Microsoft.VisualStudio.Modeling.SerializationResult serializationResult, nHydrateModel modelRoot, string modelFileName, nHydrateDiagram diagram, string diagramFileName, Encoding encoding, bool writeOptionalPropertiesWithDefaultValue)
        {
            var mainInfo = new FileInfo(modelFileName);
            modelRoot.ModelFileName = modelFileName;
            var modelName = mainInfo.Name.Replace(".nhydrate", ".model");

            if (modelRoot.ModelToDisk)
            {
                nHydrate2.Dsl.Custom.SQLFileManagement.SaveToDisk(modelRoot, mainInfo.DirectoryName, modelName, diagram);
            }
            else
            {
                try
                {
                    var f = nHydrate2.Dsl.Custom.SQLFileManagement.GetModelFolder(mainInfo.DirectoryName, modelName);
                    if (Directory.Exists(f)) Directory.Delete(f, true);
                }
                catch
                {
                }
            }

            base.SaveModelAndDiagram(serializationResult, modelRoot, modelFileName, diagram, diagramFileName, encoding, writeOptionalPropertiesWithDefaultValue);

            //Model File
            if (modelRoot.ModelToDisk)
            {
                var document = new XmlDocument();
                document.Load(modelFileName);

                //Remove entire node for Views, Stored Procedures, and Functions
                for (var ii = document.DocumentElement.ChildNodes.Count - 1; ii >= 0; ii--)
                {
                    var n = document.DocumentElement.ChildNodes[ii];
                    document.DocumentElement.RemoveChild(n);
                }

                document.Save(modelFileName);
            }

            //Diagram File
            //Now gut the diagram file
            var diagramFile = modelFileName + ".diagram";
            if (modelRoot.ModelToDisk)
            {
                if (File.Exists(diagramFile))
                {
                    var document = new XmlDocument();
                    document.Load(diagramFile);

                    //Remove all child nodes
                    var n = document.DocumentElement.SelectSingleNode("nestedChildShapes");
                    if (n != null)
                    {
                        document.DocumentElement.RemoveChild(n);
                        document.Save(diagramFile);
                    }

                    ////Reset position attributes
                    //var nlist = document.DocumentElement.SelectNodes("//entityShape");
                    //foreach (XmlElement node in nlist)
                    //{
                    //  //XmlHelper.RemoveAttribute(node, "absoluteBounds");
                    //  XmlHelper.AddAttribute(node, "absoluteBounds", "0, 0, 0, 0");
                    //  var clist = node.SelectNodes("nestedChildShapes/elementListCompartment");
                    //  foreach (XmlElement node2 in clist)
                    //  {
                    //    //XmlHelper.RemoveAttribute(node2, "absoluteBounds");
                    //    XmlHelper.AddAttribute(node2, "absoluteBounds", "0, 0, 0, 0");
                    //  }
                    //}
                    //document.Save(diagramFile);
                }
            }
            else
            {
                //strip out all the colors from the diagram file
                if (File.Exists(diagramFile))
                {
                    var document = new XmlDocument();
                    document.Load(diagramFile);
                    var list = document.DocumentElement.SelectNodes("//elementListCompartment");
                    foreach (XmlNode n in list)
                    {
                        n.Attributes.RemoveNamedItem("fillColor");
                        n.Attributes.RemoveNamedItem("outlineColor");
                        n.Attributes.RemoveNamedItem("textColor");
                        n.Attributes.RemoveNamedItem("titleTextColor");
                        n.Attributes.RemoveNamedItem("itemTextColor");
                    }
                    document.Save(diagramFile);
                }
            }

            //Save the refactorizations
            if (modelRoot.Refactorizations.Count > 0)
            {
                var document = new XmlDocument();
                document.Load(modelFileName);
                var refactorList = XmlHelper.AddElement(document.DocumentElement, "refactorizations");
                foreach (var item in modelRoot.Refactorizations)
                {
                    var n = XmlHelper.AddElement((XmlElement)refactorList, "refactor");
                    item.ToXML((XmlElement)n);
                }
                document.Save(modelFileName);
            }
        }