/// <summary>
        /// Merges and creates a list of all of the Properties. <ScalarProperty Name="LineNum" ColumnName="LineNum" />
        /// </summary>
        /// <param name="mappingFragment">The MappingFragment.</param>
        /// <param name="entity">The Entity.</param>
        private void MergeScalarProperties(MappingFragment mappingFragment, IEntity entity)
        {
            foreach (var property in mappingFragment.ScalarProperties.Where(p => entity.Properties.Count(prop => prop.KeyName.Equals(p.ColumnName, StringComparison.OrdinalIgnoreCase)) == 0))
                _mappingDroppedEntityPropertyNames[String.Format(PROPERTY_KEY, entity.EntityKeyName, property.ColumnName)] = property.Name;

            var properties = new List<ScalarProperty>();
            foreach (var property in entity.Properties)
            {
                var prop = mappingFragment.ScalarProperties.Where(p => p.ColumnName.Equals(property.KeyName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

                ScalarProperty complexProperty = null;
                if(mappingFragment.ComplexProperties != null && mappingFragment.ComplexProperties.Count > 0)
                {
                    foreach (ComplexProperty complexProp in mappingFragment.ComplexProperties)
                    {
                        complexProperty = complexProp.ScalarProperties.Where(p => p.ColumnName.Equals(property.KeyName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                        if (complexProperty != null) break;
                    }
                }

                if (prop == null)
                {
                    // The property doesn't exist so lets create it.
                    prop = new ScalarProperty() { Name = property.Name };
                }
                else if (!property.Name.Equals(prop.Name, StringComparison.OrdinalIgnoreCase)) // Column matches that in the database.. If the names are different, it wins.
                {
                    // The propertyName has been updated.
                    // TODO: Is there a better way to find out if they renamed the Property?
                    prop.Name = prop.Name;
                }
                else
                {
                    // Update the propertyName so it is always current with SchemaHelper.
                    prop.Name = property.Name;
                }

                // Forces complex properties to win.
                if (complexProperty != null)
                {
                    complexProperty.ColumnName = property.KeyName;
                    _mappingEntityPropertyNames[String.Format("{0}-{1}", entity.Name, property.KeyName)] = complexProperty.Name;
                }
                else
                {
                    prop.ColumnName = property.KeyName;
                    _mappingEntityPropertyNames[String.Format("{0}-{1}", entity.Name, property.KeyName)] = prop.Name;

                    if (!ExcludeProperty(property as ISchemaProperty))
                        properties.Add(prop);
                }
            }

            mappingFragment.ScalarProperties = properties.Distinct().ToList();
        }
        /// <summary>
        /// 2.1: Update all of the Mapping Entities so we can guarantee and verify that our Mapping Associations End's exist.
        /// 
        /// <EntitySetMapping Name="Account">
        ///  <EntityTypeMapping TypeName="PetShopModel.Account">
        ///   <MappingFragment StoreEntitySet="Account">
        ///     <ScalarProperty Name="AccountId" ColumnName="AccountId" />
        ///   </MappingFragment>
        ///  </EntityTypeMapping>
        /// </EntitySetMapping>
        /// 
        /// </summary>
        /// <param name="entity">The Entity.</param>
        private void CreateMappingEntity(IEntity entity)
        {
            if (_mappingEntitys.Contains(entity.Name))
            {
                Debug.WriteLine(String.Format("Already Processed Mapping Model Entity {0}", entity.Name), MappingCategory);
                return;
            }

            //<EntitySetMapping Name="Categories">
            #region Validate that an EntitySet Exists in the MappingStorageContainer.

            var entitySet = MappingEntityContainer.EntitySetMappings.Where(e =>
                entity.Name.Equals(e.Name, StringComparison.OrdinalIgnoreCase) || // Safe Name.
                entity.EntityKeyName.Equals(e.Name, StringComparison.OrdinalIgnoreCase) || // Database Name.
                (e.EntityTypeMappings.Count > 0 &&
                    e.EntityTypeMappings.Count(et => et.TypeName.Equals(String.Concat(ConceptualSchema.Namespace, ".", entity.Name), StringComparison.OrdinalIgnoreCase)) > 0 ||
                    e.EntityTypeMappings.Count(et => et.TypeName.Equals(String.Concat(ConceptualSchema.Namespace, ".", entity.EntityKeyName), StringComparison.OrdinalIgnoreCase)) > 0 ||
                    e.EntityTypeMappings.Count(et => et.TypeName.Equals(String.Format("IsTypeOf({0}.{1})", ConceptualSchema.Namespace, entity.Name), StringComparison.OrdinalIgnoreCase)) > 0 ||
                    e.EntityTypeMappings.Count(et => et.TypeName.Equals(String.Format("IsTypeOf({0}.{1})", ConceptualSchema.Namespace, entity.EntityKeyName), StringComparison.OrdinalIgnoreCase)) > 0 ||
                    e.EntityTypeMappings.Count(et => et.MappingFragments.Count > 0 && et.MappingFragments.Count(mf => mf.StoreEntitySet.Equals(entity.EntityKeyName, StringComparison.OrdinalIgnoreCase)) > 0) > 0)
                    ).FirstOrDefault();

            //NOTE: We could also possibly look up the table name by looking at the StorageModel's EntitySet Tables Property.

            // If the Entity Set does not exist than create a new one.
            if (entitySet == null)
            {
                entitySet = new EntitySetMapping() { Name = entity.Name };
                MappingEntityContainer.EntitySetMappings.Add(entitySet);
            }

            #endregion

            //<EntityTypeMapping TypeName="PetShopModel1.Category">
            //<EntityTypeMapping TypeName="PetShopModel.CategoryChanged">
            #region Validate the EntityType Mapping

            string entityName = entity.Name;
            var mapping = entitySet.EntityTypeMappings.FirstOrDefault();
            if (mapping == null)
            {
                mapping = new EntityTypeMapping() { TypeName = String.Concat(ConceptualSchema.Namespace, ".", entity.Name) };
                entitySet.EntityTypeMappings.Add(mapping);
            }
            else if (!String.IsNullOrEmpty(mapping.TypeName))
            {
                entityName = mapping.TypeName.Replace("IsTypeOf(", "").Replace(String.Format("{0}.", ConceptualSchema.Namespace), "").Replace(")", "");
                entityName = entityName.Equals(entity.Name, StringComparison.OrdinalIgnoreCase)
                                 ? entity.Name
                                 : entityName;
            }

            entitySet.Name = entityName;

            // Check for inheritance.
            mapping.TypeName = mapping.TypeName != null && mapping.TypeName.StartsWith("IsTypeOf") ?
                String.Format("IsTypeOf({0}.{1})", ConceptualSchema.Namespace, entityName) :
                String.Concat(ConceptualSchema.Namespace, ".", entityName);

            _mappingEntityNames.Add(entity.EntityKey(), entityName);

            // <MappingFragment StoreEntitySet="Category">
            //  <ScalarProperty Name="CategoryId" ColumnName="CategoryId" />
            //</MappingFragment>
            var mappingFragment = mapping.MappingFragments.Where(m => m.StoreEntitySet.Equals(entity.Name) || m.StoreEntitySet.Equals(entity.EntityKeyName)).FirstOrDefault();
            if (mappingFragment == null)
            {
                mappingFragment = new MappingFragment() { StoreEntitySet = entity.Name };
                mapping.MappingFragments.Add(mappingFragment);
            }

            mappingFragment.StoreEntitySet = entity.EntityKeyName;

            //<ScalarProperty Name="LineNum" ColumnName="LineNum" />
            MergeScalarProperties(mappingFragment, entity);

            #endregion

            _mappingEntitys.Add(entity.Name);
        }