private void CreateFunctionMappingComplexTypeMapping(CommandEntity entity, FunctionImportMapping importMapping)
        {
            //<ResultMapping>
            //  <ComplexTypeMapping TypeName="PetShopModel.GetCategoryById_Result">
            string entityName = String.Concat(entity.Name, "Result");
            var mapping = importMapping.ResultMapping != null && importMapping.ResultMapping.ComplexTypeMappings != null
                              ? importMapping.ResultMapping.ComplexTypeMappings.FirstOrDefault()
                              : null;

            if (mapping == null)
            {
                importMapping.ResultMapping = new FunctionImportMappingResultMapping()
                                                  {
                                                      ComplexTypeMappings = new List<FunctionImportComplexTypeMapping>()
                                                  };

                mapping = new FunctionImportComplexTypeMapping() { TypeName = String.Concat(ConceptualSchema.Namespace, ".", entityName) };
                importMapping.ResultMapping.ComplexTypeMappings.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;
            }

            if(ConceptualSchema.ComplexTypes.Count(c => c.Name.Equals(entityName, StringComparison.OrdinalIgnoreCase)) == 0)
                entityName = String.Concat(entity.Name, "Result");

            // Check for inheritance.
            mapping.TypeName = String.Format("{0}.{1}", ConceptualSchema.Namespace, entityName);

            _mappingEntityNames.Add(entity.EntityKey(), importMapping.FunctionImportName);
            _mappingEntityNames.Add(entity.EntityKey() + "complex", entityName);

            //<ComplexTypeMapping TypeName="PetShopModel.GetCategoryById_Result">
            //  <ScalarProperty Name="CategoryId" ColumnName="CategoryId" />
            //  <ScalarProperty Name="Name" ColumnName="Name" />
            //  <ScalarProperty Name="Description" ColumnName="Descn" />
            //</ComplexTypeMapping>
            MergeScalarProperties(mapping, entity);
        }
        private void CreateFunctionMappingEntity(CommandEntity entity)
        {
            if (!Configuration.Instance.IncludeFunctions || entity.IsFunction || _mappingEntitys.Contains(entity.Name))
            {
                Debug.WriteLine(String.Format("Already Processed Mapping Model Entity {0}", entity.Name), MappingCategory);
                return;
            }

            // <FunctionImportMapping FunctionImportName="GetCategoryById" FunctionName="PetShopModel.Store.GetCategoryById" >
            #region Validate that an EntitySet Exists in the MappingStorageContainer.

            var importMapping = MappingEntityContainer.FunctionImportMappings.Where(e =>
                entity.Name.Equals(e.FunctionImportName, StringComparison.OrdinalIgnoreCase) || // Safe Name.
                entity.EntityKeyName.Equals(e.FunctionImportName, StringComparison.OrdinalIgnoreCase) || // Database Name.
                (e.ResultMapping != null && e.ResultMapping.ComplexTypeMappings.Count > 0 &&
                    (e.ResultMapping.ComplexTypeMappings.Count(et => et.TypeName.Equals(String.Concat(ConceptualSchema.Namespace, ".", entity.Name), StringComparison.OrdinalIgnoreCase)) > 0 ||
                    e.ResultMapping.ComplexTypeMappings.Count(et => et.TypeName.Equals(String.Concat(ConceptualSchema.Namespace, ".", entity.Name, "Result"), StringComparison.OrdinalIgnoreCase)) > 0 ||
                    e.ResultMapping.ComplexTypeMappings.Count(et => et.TypeName.Equals(String.Concat(ConceptualSchema.Namespace, ".", entity.EntityKeyName), StringComparison.OrdinalIgnoreCase)) > 0 ||
                    e.ResultMapping.ComplexTypeMappings.Count(et => et.TypeName.Equals(String.Concat(ConceptualSchema.Namespace, ".", entity.EntityKeyName, "Result"), StringComparison.OrdinalIgnoreCase)) > 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 (importMapping == null)
            {
                importMapping = new FunctionImportMapping
                                    {
                                        FunctionImportName = entity.Name,
                                        ResultMapping = new FunctionImportMappingResultMapping()
                                    };

                MappingEntityContainer.FunctionImportMappings.Add(importMapping);
            }

            importMapping.FunctionName = String.Concat(StorageSchema.Namespace, ".", entity.EntityKeyName);

            if (String.IsNullOrEmpty(importMapping.FunctionImportName) || !ConceptualSchemaEntityContainer.FunctionImports.Exists(importMapping.FunctionImportName))
                importMapping.FunctionImportName = entity.Name;

            #endregion

            if (entity.IsStronglyTypedAssociatedEntity)
            {
                if (importMapping.ResultMapping != null && importMapping.ResultMapping.ComplexTypeMappings != null)
                    importMapping.ResultMapping.ComplexTypeMappings = null;

                CreateFunctionMappingEntityTypeMapping(entity, importMapping);
            }
            else if (entity.Properties.Count > 0)
                CreateFunctionMappingComplexTypeMapping(entity, importMapping);
            else if(importMapping.ResultMapping != null && importMapping.ResultMapping.ComplexTypeMappings != null)
                importMapping.ResultMapping.ComplexTypeMappings.Clear();

            _mappingEntitys.Add(entity.Name);
        }