private static void UpdateSentinelValuesInFacets(ref TypeUsage typeUsage)
        {
            PrimitiveType edmType = (PrimitiveType)typeUsage.EdmType;

            if (edmType.PrimitiveTypeKind != PrimitiveTypeKind.String && edmType.PrimitiveTypeKind != PrimitiveTypeKind.Binary || !Helper.IsUnboundedFacetValue(typeUsage.Facets["MaxLength"]))
            {
                return;
            }
            typeUsage = typeUsage.ShallowCopy(new FacetValues()
            {
                MaxLength = (FacetValueContainer <int?>)Helper.GetFacet((IEnumerable <FacetDescription>)edmType.FacetDescriptions, "MaxLength").MaxValue
            });
        }
        internal bool TryGetType(
            PrimitiveTypeKind primitiveTypeKind,
            IEnumerable <Facet> facets,
            out PrimitiveType type)
        {
            type = (PrimitiveType)null;
            List <PrimitiveType> primitiveTypeList = EntityUtil.CheckArgumentOutOfRange <List <PrimitiveType> >(this._primitiveTypeMap, (int)primitiveTypeKind, nameof(primitiveTypeKind));

            if (primitiveTypeList == null || 0 >= primitiveTypeList.Count)
            {
                return(false);
            }
            if (primitiveTypeList.Count == 1)
            {
                type = primitiveTypeList[0];
                return(true);
            }
            if (facets == null)
            {
                FacetDescription[] facetDescriptions = EdmProviderManifest.GetInitialFacetDescriptions(primitiveTypeKind);
                if (facetDescriptions == null)
                {
                    type = primitiveTypeList[0];
                    return(true);
                }
                facets = (IEnumerable <Facet>)CacheForPrimitiveTypes.CreateInitialFacets(facetDescriptions);
            }
            bool flag = false;

            foreach (Facet facet in facets)
            {
                if ((primitiveTypeKind == PrimitiveTypeKind.String || primitiveTypeKind == PrimitiveTypeKind.Binary) && (facet.Value != null && facet.Name == "MaxLength") && Helper.IsUnboundedFacetValue(facet))
                {
                    flag = true;
                }
            }
            int num1 = 0;

            foreach (PrimitiveType primitiveType in primitiveTypeList)
            {
                if (flag)
                {
                    if (type == null)
                    {
                        type = primitiveType;
                        num1 = Helper.GetFacet((IEnumerable <FacetDescription>)primitiveType.FacetDescriptions, "MaxLength").MaxValue.Value;
                    }
                    else
                    {
                        int num2 = Helper.GetFacet((IEnumerable <FacetDescription>)primitiveType.FacetDescriptions, "MaxLength").MaxValue.Value;
                        if (num2 > num1)
                        {
                            type = primitiveType;
                            num1 = num2;
                        }
                    }
                }
                else
                {
                    type = primitiveType;
                    break;
                }
            }
            return(true);
        }
        /// <summary>
        ///     Try and get the mapped type for the given primitiveTypeKind in the given dataspace
        /// </summary>
        /// <param name="primitiveTypeKind"> The primitive type kind of the primitive type to retrieve </param>
        /// <param name="facets"> The facets to use in picking the primitive type </param>
        /// <param name="type"> The resulting type </param>
        /// <returns> Whether a type was retrieved or not </returns>
        internal bool TryGetType(PrimitiveTypeKind primitiveTypeKind, IEnumerable <Facet> facets, out PrimitiveType type)
        {
            type = null;

            // Now, see if we have any types for this model type, if so, loop through to find the best matching one
            var primitiveTypes = EntityUtil.CheckArgumentOutOfRange(_primitiveTypeMap, (int)primitiveTypeKind, "primitiveTypeKind");

            if ((null != primitiveTypes) &&
                (0 < primitiveTypes.Count))
            {
                if (primitiveTypes.Count == 1)
                {
                    type = primitiveTypes[0];
                    return(true);
                }

                if (facets == null)
                {
                    var facetDescriptions = EdmProviderManifest.GetInitialFacetDescriptions(primitiveTypeKind);
                    if (facetDescriptions == null)
                    {
                        type = primitiveTypes[0];
                        return(true);
                    }

                    Debug.Assert(facetDescriptions.Length > 0);
                    facets = CreateInitialFacets(facetDescriptions);
                }

                Debug.Assert(type == null, "type must be null here");
                var isMaxLengthSentinel = false;

                // Create a dictionary of facets for easy lookup
                foreach (var facet in facets)
                {
                    if ((primitiveTypeKind == PrimitiveTypeKind.String ||
                         primitiveTypeKind == PrimitiveTypeKind.Binary)
                        &&
                        facet.Value != null
                        &&
                        facet.Name == DbProviderManifest.MaxLengthFacetName
                        &&
                        Helper.IsUnboundedFacetValue(facet))
                    {
                        // MaxLength has the sentinel value. So this facet need not be added.
                        isMaxLengthSentinel = true;
                        continue;
                    }
                }

                var maxLength = 0;
                // Find a primitive type with the matching constraint
                foreach (var primitiveType in primitiveTypes)
                {
                    if (isMaxLengthSentinel)
                    {
                        if (type == null)
                        {
                            type      = primitiveType;
                            maxLength =
                                Helper.GetFacet(primitiveType.FacetDescriptions, DbProviderManifest.MaxLengthFacetName).MaxValue.Value;
                        }
                        else
                        {
                            var newMaxLength =
                                Helper.GetFacet(primitiveType.FacetDescriptions, DbProviderManifest.MaxLengthFacetName).MaxValue.Value;
                            if (newMaxLength > maxLength)
                            {
                                type      = primitiveType;
                                maxLength = newMaxLength;
                            }
                        }
                    }
                    else
                    {
                        type = primitiveType;
                        break;
                    }
                }

                Debug.Assert(type != null);
                return(true);
            }

            return(false);
        }