private void AddFunction(
            PrimitiveTypeKind returnType, string functionName, KeyValuePair <string, PrimitiveTypeKind>[] parameterDefinitions)
        {
            var returnParameter = CreateReturnParameter(returnType);
            var parameters      = parameterDefinitions.Select(paramDef => CreateParameter(paramDef.Value, paramDef.Key)).ToArray();

            var function = new EdmFunction(
                functionName,
                EdmConstants.EdmNamespace,
                DataSpace.CSpace,
                new EdmFunctionPayload
            {
                IsBuiltIn              = true,
                ReturnParameters       = new[] { returnParameter },
                Parameters             = parameters,
                IsFromProviderManifest = true,
            });

            function.SetReadOnly();

            functions.Add(function);
        }
Esempio n. 2
0
        public void WriteFunctionImportMappingElement_writes_result_mapping_for_non_composable_functions_mapped_explicitly_to_EntityType()
        {
            var typeUsage =
                TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));

            var entityTypeProperty1 = new EdmProperty("ETProperty1", typeUsage);
            var entityTypeProperty2 = new EdmProperty("ETProperty2", typeUsage);

            var entityType = new EntityType("ET", "Ns", DataSpace.CSpace);

            entityType.AddMember(entityTypeProperty1);
            entityType.AddMember(entityTypeProperty2);

            var functionImport =
                new EdmFunction(
                    "f_c", "Ns", DataSpace.CSpace,
                    new EdmFunctionPayload
            {
                IsComposable     = false,
                IsFunctionImport = true,
                ReturnParameters =
                    new[]
                {
                    new FunctionParameter(
                        "ReturnValue",
                        TypeUsage.CreateDefaultTypeUsage(entityType.GetCollectionType()),
                        ParameterMode.ReturnValue)
                },
            });

            typeUsage = ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(typeUsage);
            var rowTypeProperty1 = new EdmProperty("RTProperty1", typeUsage);
            var rowTypeProperty2 = new EdmProperty("RTProperty2", typeUsage);
            var rowType          = new RowType(new[] { rowTypeProperty1, rowTypeProperty2 });

            var storeFunction =
                new EdmFunction(
                    "f_s", "Ns.Store", DataSpace.SSpace,
                    new EdmFunctionPayload
            {
                IsComposable     = false,
                ReturnParameters =
                    new[]
                {
                    new FunctionParameter(
                        "Return",
                        TypeUsage.CreateDefaultTypeUsage(rowType),
                        ParameterMode.ReturnValue)
                },
            });

            var functionImportResultMapping = new FunctionImportResultMapping();

            functionImportResultMapping.AddTypeMapping(
                new FunctionImportEntityTypeMapping(
                    new EntityType[0],
                    new [] { entityType },
                    new Collections.ObjectModel.Collection <FunctionImportReturnTypePropertyMapping>()
            {
                new FunctionImportReturnTypeScalarPropertyMapping("ETProperty1", "RTProperty1"),
                new FunctionImportReturnTypeScalarPropertyMapping("ETProperty2", "RTProperty2")
            },
                    new FunctionImportEntityTypeMappingCondition[]
            {
                new FunctionImportEntityTypeMappingConditionIsNull("RTProperty1", false),
                new FunctionImportEntityTypeMappingConditionValue("RTProperty2", 4),
                new FunctionImportEntityTypeMappingConditionValue("FakeCondition", true)
            }
                    ));

            var mappingItemCollection =
                new StorageMappingItemCollection(
                    new EdmItemCollection(EdmModel.CreateConceptualModel()),
                    new StoreItemCollection(
                        EdmModel.CreateStoreModel(ProviderRegistry.Sql2008_ProviderInfo, ProviderRegistry.Sql2008_ProviderManifest)),
                    new string[0]);

            var containerMapping =
                new EntityContainerMapping(
                    new EntityContainer("C", DataSpace.CSpace), new EntityContainer("S", DataSpace.SSpace), mappingItemCollection, false);

            var functionImportMapping =
                new FunctionImportMappingNonComposable(
                    functionImport,
                    storeFunction,
                    new []
            {
                functionImportResultMapping
            },
                    containerMapping);

            containerMapping.AddFunctionImportMapping(functionImportMapping);

            var fixture = new Fixture();

            fixture.Writer.WriteFunctionImportMappingElement(functionImportMapping);
            Assert.Equal(
                @"<FunctionImportMapping FunctionName=""Ns.Store.f_s"" FunctionImportName=""f_c"">
  <ResultMapping>
    <EntityTypeMapping TypeName=""Ns.ET"">
      <ScalarProperty Name=""ETProperty1"" ColumnName=""RTProperty1"" />
      <ScalarProperty Name=""ETProperty2"" ColumnName=""RTProperty2"" />
      <Condition ColumnName=""RTProperty1"" IsNull=""false"" />
      <Condition ColumnName=""RTProperty2"" Value=""4"" />
      <Condition ColumnName=""FakeCondition"" Value=""1"" />
    </EntityTypeMapping>
  </ResultMapping>
</FunctionImportMapping>",
                fixture.ToString());
        }
Esempio n. 3
0
        public void WriteFunctionImportMappingElement_does_not_write_result_mapping_for_non_composable_functions_mapped_implicitly()
        {
            var typeUsage =
                TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));

            var complexTypeProperty1 = new EdmProperty("CTProperty1", typeUsage);
            var complexTypeProperty2 = new EdmProperty("CTProperty2", typeUsage);

            var complexType = new ComplexType("CT", "Ns", DataSpace.CSpace);

            complexType.AddMember(complexTypeProperty1);
            complexType.AddMember(complexTypeProperty2);

            var functionImport =
                new EdmFunction(
                    "f_c", "Ns", DataSpace.CSpace,
                    new EdmFunctionPayload
            {
                IsComposable     = false,
                IsFunctionImport = true,
                ReturnParameters =
                    new[]
                {
                    new FunctionParameter(
                        "ReturnValue",
                        TypeUsage.CreateDefaultTypeUsage(complexType.GetCollectionType()),
                        ParameterMode.ReturnValue)
                },
            });

            typeUsage = ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(typeUsage);
            var rowTypeProperty1 = new EdmProperty("RTProperty1", typeUsage);
            var rowTypeProperty2 = new EdmProperty("RTProperty2", typeUsage);
            var rowType          = new RowType(new[] { rowTypeProperty1, rowTypeProperty2 });

            var storeFunction =
                new EdmFunction(
                    "f_s", "Ns.Store", DataSpace.SSpace,
                    new EdmFunctionPayload
            {
                IsComposable     = false,
                ReturnParameters =
                    new[]
                {
                    new FunctionParameter(
                        "Return",
                        TypeUsage.CreateDefaultTypeUsage(rowType),
                        ParameterMode.ReturnValue)
                },
            });

            var functionImportMapping =
                new FunctionImportMappingNonComposable(
                    functionImport,
                    storeFunction,
                    new FunctionImportResultMapping[0],
                    new EntityContainerMapping(new EntityContainer("C", DataSpace.SSpace)));

            var fixture = new Fixture();

            fixture.Writer.WriteFunctionImportMappingElement(functionImportMapping);
            Assert.Equal(
                @"<FunctionImportMapping FunctionName=""Ns.Store.f_s"" FunctionImportName=""f_c"" />",
                fixture.ToString());
        }
Esempio n. 4
0
        public void WriteEntityContainerMappingElement_should_write_function_import_elements_mapped_to_EntityType()
        {
            var typeUsage =
                TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));

            var entityTypeProperty1 = new EdmProperty("ETProperty1", typeUsage);
            var entityTypeProperty2 = new EdmProperty("ETProperty2", typeUsage);

            var entityType = new EntityType("ET", "Ns", DataSpace.CSpace);

            entityType.AddMember(entityTypeProperty1);
            entityType.AddMember(entityTypeProperty2);

            var functionImport =
                new EdmFunction(
                    "f_c", "Ns", DataSpace.CSpace,
                    new EdmFunctionPayload
            {
                IsComposable     = true,
                IsFunctionImport = true,
                ReturnParameters =
                    new[]
                {
                    new FunctionParameter(
                        "ReturnValue",
                        TypeUsage.CreateDefaultTypeUsage(entityType.GetCollectionType()),
                        ParameterMode.ReturnValue)
                },
                Parameters =
                    new[]
                {
                    new FunctionParameter("param", typeUsage, ParameterMode.Out)
                }
            });

            typeUsage = ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(typeUsage);
            var rowTypeProperty1 = new EdmProperty("RTProperty1", typeUsage);
            var rowTypeProperty2 = new EdmProperty("RTProperty2", typeUsage);
            var rowType          = new RowType(new[] { rowTypeProperty1, rowTypeProperty2 });

            var storeFunction =
                new EdmFunction(
                    "f_s", "Ns.Store", DataSpace.SSpace,
                    new EdmFunctionPayload
            {
                ReturnParameters =
                    new[]
                {
                    new FunctionParameter(
                        "Return",
                        TypeUsage.CreateDefaultTypeUsage(rowType),
                        ParameterMode.ReturnValue)
                },
                Parameters =
                    new[]
                {
                    new FunctionParameter("param", typeUsage, ParameterMode.Out)
                }
            });

            var structuralTypeMapping =
                new Tuple <StructuralType, List <ConditionPropertyMapping>, List <PropertyMapping> >(
                    entityType, new List <ConditionPropertyMapping>(), new List <PropertyMapping>());

            structuralTypeMapping.Item2.Add(new ConditionPropertyMapping(rowTypeProperty1, null, false));

            structuralTypeMapping.Item3.Add(new ScalarPropertyMapping(entityTypeProperty1, rowTypeProperty1));
            structuralTypeMapping.Item3.Add(new ScalarPropertyMapping(entityTypeProperty2, rowTypeProperty2));

            var functionImportMapping = new FunctionImportMappingComposable(
                functionImport,
                storeFunction,
                new List <Tuple <StructuralType, List <ConditionPropertyMapping>, List <PropertyMapping> > >
            {
                structuralTypeMapping
            });

            var containerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.SSpace));

            containerMapping.AddFunctionImportMapping(functionImportMapping);

            var fixture = new Fixture();

            fixture.Writer.WriteEntityContainerMappingElement(containerMapping);

            Assert.Equal(
                @"<EntityContainerMapping StorageEntityContainer="""" CdmEntityContainer=""C"">
  <FunctionImportMapping FunctionName=""Ns.Store.f_s"" FunctionImportName=""f_c"">
    <ResultMapping>
      <EntityTypeMapping TypeName=""Ns.ET"">
        <Condition IsNull=""false"" ColumnName=""RTProperty1"" />
        <ScalarProperty Name=""ETProperty1"" ColumnName=""RTProperty1"" />
        <ScalarProperty Name=""ETProperty2"" ColumnName=""RTProperty2"" />
      </EntityTypeMapping>
    </ResultMapping>
  </FunctionImportMapping>
</EntityContainerMapping>",
                fixture.ToString());
        }
Esempio n. 5
0
 protected internal override void VisitEdmFunction(EdmFunction item)
 {
     this._schemaWriter.WriteFunctionElementHeader(item);
     base.VisitEdmFunction(item);
     this._schemaWriter.WriteEndElement();
 }
Esempio n. 6
0
        // <summary>
        // Convert the S type function parameters and returnType to C types.
        // </summary>
        internal static EdmFunction ConvertFunctionSignatureToCType(EdmFunction sTypeFunction)
        {
            Debug.Assert(sTypeFunction.DataSpace == DataSpace.SSpace, "sTypeFunction.DataSpace == Edm.DataSpace.SSpace");

            if (sTypeFunction.IsFromProviderManifest)
            {
                return(sTypeFunction);
            }

            FunctionParameter returnParameter = null;

            if (sTypeFunction.ReturnParameter != null)
            {
                var edmTypeUsageReturnParameter =
                    MetadataHelper.ConvertStoreTypeUsageToEdmTypeUsage(sTypeFunction.ReturnParameter.TypeUsage);

                returnParameter =
                    new FunctionParameter(
                        sTypeFunction.ReturnParameter.Name,
                        edmTypeUsageReturnParameter,
                        sTypeFunction.ReturnParameter.GetParameterMode());
            }

            var parameters = new List <FunctionParameter>();

            if (sTypeFunction.Parameters.Count > 0)
            {
                foreach (var parameter in sTypeFunction.Parameters)
                {
                    var edmTypeUsage = MetadataHelper.ConvertStoreTypeUsageToEdmTypeUsage(parameter.TypeUsage);

                    var edmTypeParameter = new FunctionParameter(parameter.Name, edmTypeUsage, parameter.GetParameterMode());
                    parameters.Add(edmTypeParameter);
                }
            }

            var returnParameters =
                returnParameter == null ? new FunctionParameter[0] : new[] { returnParameter };
            var edmFunction = new EdmFunction(
                sTypeFunction.Name,
                sTypeFunction.NamespaceName,
                DataSpace.CSpace,
                new EdmFunctionPayload
            {
                Schema                 = sTypeFunction.Schema,
                StoreFunctionName      = sTypeFunction.StoreFunctionNameAttribute,
                CommandText            = sTypeFunction.CommandTextAttribute,
                IsAggregate            = sTypeFunction.AggregateAttribute,
                IsBuiltIn              = sTypeFunction.BuiltInAttribute,
                IsNiladic              = sTypeFunction.NiladicFunctionAttribute,
                IsComposable           = sTypeFunction.IsComposableAttribute,
                IsFromProviderManifest = sTypeFunction.IsFromProviderManifest,
                IsCachedStoreFunction  = true,
                IsFunctionImport       = sTypeFunction.IsFunctionImport,
                ReturnParameters       = returnParameters,
                Parameters             = parameters.ToArray(),
                ParameterTypeSemantics = sTypeFunction.ParameterTypeSemanticsAttribute,
            });

            edmFunction.SetReadOnly();

            return(edmFunction);
        }
Esempio n. 7
0
 internal EdmFunction ConvertToCTypeFunction(EdmFunction sTypeFunction)
 {
     return(_cachedCTypeFunction.Evaluate(sTypeFunction));
 }
 /// <summary>Adds a function to the model.</summary>
 /// <param name="item">The EdmFunction instance to be added.</param>
 public void AddItem(EdmFunction item)
 {
     Check.NotNull <EdmFunction>(item, nameof(item));
     this.ValidateSpace((EdmType)item);
     this._functions.Add(item);
 }
 internal void AddFunctionImport(EdmFunction function)
 {
     DebugCheck.NotNull(function);
     Debug.Assert(function.IsFunctionImport, "function.IsFunctionImport");
     _functionImports.Source.Add(function);
 }
Esempio n. 10
0
        /// <summary>
        /// Removes a function from the model.
        /// </summary>
        /// <param name="item">The EdmFunction instance to be removed.</param>
        public void RemoveItem(EdmFunction item)
        {
            Check.NotNull(item, "item");

            _functions.Remove(item);
        }
        private static EdmFunction ConvertToFunction(
            System.Data.Entity.Core.SchemaObjectModel.Function somFunction,
            DbProviderManifest providerManifest,
            Converter.ConversionCache convertedItemCache,
            EntityContainer functionImportEntityContainer,
            Dictionary <SchemaElement, GlobalItem> newGlobalItems)
        {
            GlobalItem globalItem = (GlobalItem)null;

            if (!somFunction.IsFunctionImport && newGlobalItems.TryGetValue((SchemaElement)somFunction, out globalItem))
            {
                return((EdmFunction)globalItem);
            }
            bool areConvertingForProviderManifest           = somFunction.Schema.DataModel == SchemaDataModelOption.ProviderManifestModel;
            List <FunctionParameter> functionParameterList1 = new List <FunctionParameter>();

            if (somFunction.ReturnTypeList != null)
            {
                int num = 0;
                foreach (ReturnType returnType in (IEnumerable <ReturnType>)somFunction.ReturnTypeList)
                {
                    TypeUsage functionTypeUsage = Converter.GetFunctionTypeUsage(somFunction is ModelFunction, somFunction, (FacetEnabledSchemaElement)returnType, providerManifest, areConvertingForProviderManifest, returnType.Type, returnType.CollectionKind, returnType.IsRefType, convertedItemCache, newGlobalItems);
                    if (functionTypeUsage == null)
                    {
                        return((EdmFunction)null);
                    }
                    string str = num == 0 ? string.Empty : num.ToString((IFormatProvider)CultureInfo.InvariantCulture);
                    ++num;
                    FunctionParameter functionParameter = new FunctionParameter("ReturnType" + str, functionTypeUsage, ParameterMode.ReturnValue);
                    Converter.AddOtherContent((SchemaElement)returnType, (MetadataItem)functionParameter);
                    functionParameterList1.Add(functionParameter);
                }
            }
            else if (somFunction.Type != null)
            {
                TypeUsage functionTypeUsage = Converter.GetFunctionTypeUsage(somFunction is ModelFunction, somFunction, (FacetEnabledSchemaElement)null, providerManifest, areConvertingForProviderManifest, somFunction.Type, somFunction.CollectionKind, somFunction.IsReturnAttributeReftype, convertedItemCache, newGlobalItems);
                if (functionTypeUsage == null)
                {
                    return((EdmFunction)null);
                }
                functionParameterList1.Add(new FunctionParameter("ReturnType", functionTypeUsage, ParameterMode.ReturnValue));
            }
            EntitySet[] entitySetArray = (EntitySet[])null;
            string      name;

            if (somFunction.IsFunctionImport)
            {
                FunctionImportElement functionImportElement = (FunctionImportElement)somFunction;
                name = functionImportElement.Container.Name;
                if (functionImportElement.EntitySet != null)
                {
                    EntityContainer container = functionImportEntityContainer;
                    entitySetArray = new EntitySet[1]
                    {
                        Converter.GetEntitySet(functionImportElement.EntitySet, container)
                    };
                }
                else if (functionImportElement.ReturnTypeList != null)
                {
                    EntityContainer entityContainer = functionImportEntityContainer;
                    entitySetArray = functionImportElement.ReturnTypeList.Select <ReturnType, EntitySet>((Func <ReturnType, EntitySet>)(returnType =>
                    {
                        if (returnType.EntitySet == null)
                        {
                            return((EntitySet)null);
                        }
                        return(Converter.GetEntitySet(returnType.EntitySet, functionImportEntityContainer));
                    })).ToArray <EntitySet>();
                }
            }
            else
            {
                name = somFunction.Namespace;
            }
            List <FunctionParameter> functionParameterList2 = new List <FunctionParameter>();

            foreach (Parameter parameter in somFunction.Parameters)
            {
                TypeUsage functionTypeUsage = Converter.GetFunctionTypeUsage(somFunction is ModelFunction, somFunction, (FacetEnabledSchemaElement)parameter, providerManifest, areConvertingForProviderManifest, parameter.Type, parameter.CollectionKind, parameter.IsRefType, convertedItemCache, newGlobalItems);
                if (functionTypeUsage == null)
                {
                    return((EdmFunction)null);
                }
                FunctionParameter functionParameter = new FunctionParameter(parameter.Name, functionTypeUsage, Converter.GetParameterMode(parameter.ParameterDirection));
                Converter.AddOtherContent((SchemaElement)parameter, (MetadataItem)functionParameter);
                if (parameter.Documentation != null)
                {
                    functionParameter.Documentation = Converter.ConvertToDocumentation(parameter.Documentation);
                }
                functionParameterList2.Add(functionParameter);
            }
            EdmFunction edmFunction = new EdmFunction(somFunction.Name, name, Converter.GetDataSpace(providerManifest), new EdmFunctionPayload()
            {
                Schema                 = somFunction.DbSchema,
                StoreFunctionName      = somFunction.StoreFunctionName,
                CommandText            = somFunction.CommandText,
                EntitySets             = (IList <EntitySet>)entitySetArray,
                IsAggregate            = new bool?(somFunction.IsAggregate),
                IsBuiltIn              = new bool?(somFunction.IsBuiltIn),
                IsNiladic              = new bool?(somFunction.IsNiladicFunction),
                IsComposable           = new bool?(somFunction.IsComposable),
                IsFromProviderManifest = new bool?(areConvertingForProviderManifest),
                IsFunctionImport       = new bool?(somFunction.IsFunctionImport),
                ReturnParameters       = (IList <FunctionParameter>)functionParameterList1.ToArray(),
                Parameters             = (IList <FunctionParameter>)functionParameterList2.ToArray(),
                ParameterTypeSemantics = new ParameterTypeSemantics?(somFunction.ParameterTypeSemantics)
            });

            if (!somFunction.IsFunctionImport)
            {
                newGlobalItems.Add((SchemaElement)somFunction, (GlobalItem)edmFunction);
            }
            if (somFunction.Documentation != null)
            {
                edmFunction.Documentation = Converter.ConvertToDocumentation(somFunction.Documentation);
            }
            Converter.AddOtherContent((SchemaElement)somFunction, (MetadataItem)edmFunction);
            return(edmFunction);
        }
Esempio n. 12
0
 protected internal override void VisitFunctionImport(EdmFunction functionImport)
 {
     _schemaWriter.WriteFunctionImportElementHeader(functionImport);
     base.VisitFunctionImport(functionImport);
     _schemaWriter.WriteEndElement();
 }
 /// <summary>Removes a function from the model.</summary>
 /// <param name="item">The EdmFunction instance to be removed.</param>
 public void RemoveItem(EdmFunction item)
 {
     Check.NotNull <EdmFunction>(item, nameof(item));
     this._functions.Remove(item);
 }
Esempio n. 14
0
 // <summary>
 // determines if an EdmFunction is an aggregate function
 // </summary>
 internal static bool IsAggregateFunction(EdmFunction function)
 {
     return(function.AggregateAttribute);
 }
Esempio n. 15
0
 // <summary>
 // Returns the function import in the target space, for the given entity container.
 // </summary>
 internal virtual bool TryGetFunctionImport(
     EntityContainer entityContainer, String functionImportName, bool ignoreCase, out EdmFunction functionImport)
 {
     // There are no entity containers in the OSpace. So there is no mapping involved.
     // Hence the name should be a valid name in the CSpace.
     functionImport = null;
     if (ignoreCase)
     {
         functionImport =
             entityContainer.FunctionImports.Where(
                 fi => String.Equals(fi.Name, functionImportName, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
     }
     else
     {
         functionImport = entityContainer.FunctionImports.Where(fi => fi.Name == functionImportName).SingleOrDefault();
     }
     return(functionImport != null);
 }
Esempio n. 16
0
        /// <summary>
        ///     Gets the function as specified by the function key.
        ///     All parameters are assumed to be <see cref="ParameterMode.In" />.
        /// </summary>
        /// <param name="functionName"> Name of the function </param>
        /// <param name="parameterTypes"> types of the parameters </param>
        /// <param name="ignoreCase"> true for case-insensitive lookup </param>
        /// <param name="function"> The function that needs to be returned </param>
        /// <returns> The function as specified in the function key or null </returns>
        /// <exception cref="System.ArgumentNullException">if functionName or parameterTypes argument is null</exception>
        /// <exception cref="System.ArgumentException">if no function is found with the given name or with given input parameters</exception>
        internal bool TryGetFunction(string functionName, TypeUsage[] parameterTypes, bool ignoreCase, out EdmFunction function)
        {
            Check.NotNull(functionName, "functionName");
            Check.NotNull(parameterTypes, "parameterTypes");
            var        functionIdentity = EdmFunction.BuildIdentity(functionName, parameterTypes);
            GlobalItem item             = null;

            function = null;
            if (TryGetValue(functionIdentity, ignoreCase, out item) &&
                Helper.IsEdmFunction(item))
            {
                function = (EdmFunction)item;
                return(true);
            }
            return(false);
        }
        internal EdmFunction(
            string name,
            string namespaceName,
            DataSpace dataSpace,
            EdmFunctionPayload payload)
            : base(name, namespaceName, dataSpace)
        {
            this._schemaName = payload.Schema;
            IList <FunctionParameter> source = payload.ReturnParameters ?? (IList <FunctionParameter>) new FunctionParameter[0];

            foreach (FunctionParameter functionParameter in (IEnumerable <FunctionParameter>)source)
            {
                if (functionParameter == null)
                {
                    throw new ArgumentException(Strings.ADP_CollectionParameterElementIsNull((object)nameof(ReturnParameters)));
                }
                if (functionParameter.Mode != ParameterMode.ReturnValue)
                {
                    throw new ArgumentException(Strings.NonReturnParameterInReturnParameterCollection);
                }
            }
            this._returnParameters = new ReadOnlyMetadataCollection <FunctionParameter>(source.Select <FunctionParameter, FunctionParameter>((Func <FunctionParameter, FunctionParameter>)(returnParameter => SafeLink <EdmFunction> .BindChild <FunctionParameter>(this, FunctionParameter.DeclaringFunctionLinker, returnParameter))).ToList <FunctionParameter>());
            if (payload.IsAggregate.HasValue)
            {
                EdmFunction.SetFunctionAttribute(ref this._functionAttributes, EdmFunction.FunctionAttributes.Aggregate, payload.IsAggregate.Value);
            }
            if (payload.IsBuiltIn.HasValue)
            {
                EdmFunction.SetFunctionAttribute(ref this._functionAttributes, EdmFunction.FunctionAttributes.BuiltIn, payload.IsBuiltIn.Value);
            }
            if (payload.IsNiladic.HasValue)
            {
                EdmFunction.SetFunctionAttribute(ref this._functionAttributes, EdmFunction.FunctionAttributes.NiladicFunction, payload.IsNiladic.Value);
            }
            if (payload.IsComposable.HasValue)
            {
                EdmFunction.SetFunctionAttribute(ref this._functionAttributes, EdmFunction.FunctionAttributes.IsComposable, payload.IsComposable.Value);
            }
            if (payload.IsFromProviderManifest.HasValue)
            {
                EdmFunction.SetFunctionAttribute(ref this._functionAttributes, EdmFunction.FunctionAttributes.IsFromProviderManifest, payload.IsFromProviderManifest.Value);
            }
            if (payload.IsCachedStoreFunction.HasValue)
            {
                EdmFunction.SetFunctionAttribute(ref this._functionAttributes, EdmFunction.FunctionAttributes.IsCachedStoreFunction, payload.IsCachedStoreFunction.Value);
            }
            if (payload.IsFunctionImport.HasValue)
            {
                EdmFunction.SetFunctionAttribute(ref this._functionAttributes, EdmFunction.FunctionAttributes.IsFunctionImport, payload.IsFunctionImport.Value);
            }
            if (payload.ParameterTypeSemantics.HasValue)
            {
                this._parameterTypeSemantics = payload.ParameterTypeSemantics.Value;
            }
            if (payload.StoreFunctionName != null)
            {
                this._storeFunctionNameAttribute = payload.StoreFunctionName;
            }
            if (payload.EntitySets != null)
            {
                if (payload.EntitySets.Count != source.Count)
                {
                    throw new ArgumentException(Strings.NumberOfEntitySetsDoesNotMatchNumberOfReturnParameters);
                }
                this._entitySets = new ReadOnlyCollection <EntitySet>(payload.EntitySets);
            }
            else
            {
                if (this._returnParameters.Count > 1)
                {
                    throw new ArgumentException(Strings.NullEntitySetsForFunctionReturningMultipleResultSets);
                }
                this._entitySets = new ReadOnlyCollection <EntitySet>((IList <EntitySet>) this._returnParameters.Select <FunctionParameter, EntitySet>((Func <FunctionParameter, EntitySet>)(p => (EntitySet)null)).ToList <EntitySet>());
            }
            if (payload.CommandText != null)
            {
                this._commandTextAttribute = payload.CommandText;
            }
            if (payload.Parameters != null)
            {
                foreach (FunctionParameter parameter in (IEnumerable <FunctionParameter>)payload.Parameters)
                {
                    if (parameter == null)
                    {
                        throw new ArgumentException(Strings.ADP_CollectionParameterElementIsNull((object)"parameters"));
                    }
                    if (parameter.Mode == ParameterMode.ReturnValue)
                    {
                        throw new ArgumentException(Strings.ReturnParameterInInputParameterCollection);
                    }
                }
                this._parameters = (ReadOnlyMetadataCollection <FunctionParameter>) new SafeLinkCollection <EdmFunction, FunctionParameter>(this, FunctionParameter.DeclaringFunctionLinker, new MetadataCollection <FunctionParameter>((IEnumerable <FunctionParameter>)payload.Parameters));
            }
            else
            {
                this._parameters = new ReadOnlyMetadataCollection <FunctionParameter>(new MetadataCollection <FunctionParameter>());
            }
        }