/// <summary>
        /// Initializes the EPM internal information for the specified resource type and all its base types.
        /// </summary>
        /// <param name="resourceType">The resource type to build the EPM information for.</param>
        /// <remarks>If the type in question has no EPM attributes anywhere in its hierarchy this method will do nothing.</remarks>
        internal static void BuildEpm(ResourceType resourceType)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(resourceType != null, "resourceType != null");

            // Walk the base types and determine if there's EPM anywhere in there.
            bool hasEpm = false;
            ResourceType resourceTypeToTest = resourceType;
            while (resourceTypeToTest != null)
            {
                if (resourceTypeToTest.Epm() != null)
                {
                    hasEpm = true;
                    break;
                }

                resourceTypeToTest = resourceTypeToTest.BaseType;
            }

            // If there is build the EPM information for this type and add it as annotation.
            if (hasEpm)
            {
                EpmResourceTypeAnnotation epm = resourceType.Epm();
                if (epm == null)
                {
                    epm = new EpmResourceTypeAnnotation();
                    // resourceType.SetAnnotation(epm);
                }

                epm.BuildEpmForType(resourceType, resourceType);
            }
        }
        /// <summary>
        /// Initializes the EPM annotation with EPM information from the specified resource type.
        /// </summary>
        /// <param name="definingResourceType">Resource type to use the EPM infromation from.</param>
        /// <param name="affectedResourceType">Resource type for this the EPM information is being built.</param>
        private void BuildEpmForType(ResourceType definingResourceType, ResourceType affectedResourceType)
        {
            Debug.Assert(definingResourceType != null, "definingResourceType != null");
            Debug.Assert(affectedResourceType != null, "affectedResourceType != null");

            if (definingResourceType.BaseType != null)
            {
                this.BuildEpmForType(definingResourceType.BaseType, affectedResourceType);
            }

            EpmResourceTypeAnnotation epm = definingResourceType.Epm();
            if (epm != null)
            {
                foreach (EntityPropertyMappingAttribute epmAttribute in epm.AllEpmAttributes.ToList())
                {
                    this.epmSourceTree.Add(new EntityPropertyMappingInfo(epmAttribute, definingResourceType, affectedResourceType));

                    if (definingResourceType == affectedResourceType)
                    {
                        if (!PropertyExistsOnType(affectedResourceType, epmAttribute))
                        {
                            this.InheritedEpmAttributes.Add(epmAttribute);
                            this.OwnEpmAttributes.Remove(epmAttribute);
                        }
                        else
                        {
                            Debug.Assert(
                                this.OwnEpmAttributes.SingleOrDefault(attr => Object.ReferenceEquals(epmAttribute, attr)) != null,
                                "Own epmInfo should already have the given instance");
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Check whether the EPM on the specified resource type is supported in the specified version.
        /// </summary>
        /// <param name="version">The version to check.</param>
        /// <param name="resourceType">The entity resoure type to check.</param>
        internal static void CheckEntityPropertyMapping(ODataVersion version, ResourceType resourceType)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(resourceType != null, "resourceType != null");

            EpmResourceTypeAnnotation epmAnnotation = resourceType.Epm();
            if (epmAnnotation != null)
            {
                Debug.Assert(epmAnnotation.EpmTargetTree != null, "If the EPM annotation is present the EPM tree must already be initialized.");
                if (version < epmAnnotation.EpmTargetTree.MinimumODataProtocolVersion)
                {
                    throw new ODataException(
                        Strings.ODataVersionChecker_EpmVersionNotSupported(
                            resourceType.FullName, 
                            epmAnnotation.EpmTargetTree.MinimumODataProtocolVersion.VersionString(),
                            version.VersionString()));
                }
            }
        }