Example #1
0
        private IEdmNavigationProperty ComputePartner()
        {
            string partnerPropertyName = this.navigationProperty.Partner;

            IEdmEntityType targetEntityType = this.TargetEntityType;

            if (partnerPropertyName != null)
            {
                var partner = targetEntityType.FindProperty(partnerPropertyName) as IEdmNavigationProperty;

                if (partner == null)
                {
                    partner = new UnresolvedNavigationPropertyPath(targetEntityType, partnerPropertyName, this.Location);
                }

                return(partner);
            }

            foreach (IEdmNavigationProperty potentialPartner in targetEntityType.NavigationProperties())
            {
                if (potentialPartner == this)
                {
                    continue;
                }

                if (potentialPartner.Partner == this)
                {
                    return(potentialPartner);
                }
            }

            return(null);
        }
        private IEdmNavigationProperty ResolveNavigationPropertyPathForBinding(CsdlNavigationPropertyBinding binding)
        {
            Debug.Assert(binding != null, "binding != null");

            string bindingPath = binding.Path;

            Debug.Assert(bindingPath != null, "bindingPath != null");

            IEdmEntityType definingType = this.typeCache.GetValue(this, ComputeElementTypeFunc, null);

            const char Slash = '/';

            if (bindingPath.Length == 0 || bindingPath[bindingPath.Length - 1] == Slash)
            {
                // if the path did not actually contain a navigation property, then treat it as an unresolved path.
                // TODO: improve the error given in this case?
                return(new UnresolvedNavigationPropertyPath(definingType, bindingPath, binding.Location));
            }

            IEdmNavigationProperty navigationProperty;

            string[] pathSegements = bindingPath.Split(Slash);
            for (int index = 0; index < pathSegements.Length - 1; index++)
            {
                string pathSegement = pathSegements[index];
                if (pathSegement.Length == 0)
                {
                    // TODO: improve the error given in this case?
                    return(new UnresolvedNavigationPropertyPath(definingType, bindingPath, binding.Location));
                }

                IEdmEntityType derivedType = this.container.Context.FindType(pathSegement) as IEdmEntityType;
                if (derivedType == null)
                {
                    IEdmProperty property = definingType.FindProperty(pathSegement);
                    navigationProperty = property as IEdmNavigationProperty;
                    if (navigationProperty == null)
                    {
                        return(new UnresolvedNavigationPropertyPath(definingType, bindingPath, binding.Location));
                    }

                    definingType = navigationProperty.ToEntityType();
                }
                else
                {
                    definingType = derivedType;
                }
            }

            navigationProperty = definingType.FindProperty(pathSegements.Last()) as IEdmNavigationProperty;
            if (navigationProperty == null)
            {
                // TODO: improve the error given in this case?
                navigationProperty = new UnresolvedNavigationPropertyPath(definingType, bindingPath, binding.Location);
            }

            return(navigationProperty);
        }
        private IEdmNavigationProperty ComputePartner()
        {
            string partnerPropertyName = this.navigationProperty.Partner;

            IEdmEntityType targetEntityType = this.TargetEntityType;
            if (partnerPropertyName != null)
            {
                var partner = targetEntityType.FindProperty(partnerPropertyName) as IEdmNavigationProperty;

                if (partner == null)
                {
                    partner = new UnresolvedNavigationPropertyPath(targetEntityType, partnerPropertyName, this.Location);
                }

                return partner;
            }

            foreach (IEdmNavigationProperty potentialPartner in targetEntityType.NavigationProperties())
            {
                if (potentialPartner == this)
                {
                    continue;
                }

                if (potentialPartner.Partner == this)
                {
                    return potentialPartner;
                }
            }

            return null;
        }
        private IEdmNavigationProperty ResolveNavigationPropertyPathForBinding(CsdlNavigationPropertyBinding binding)
        {
            Debug.Assert(binding != null, "binding != null");

            string bindingPath = binding.Path;
            Debug.Assert(bindingPath != null, "bindingPath != null");

            IEdmEntityType definingType = this.typeCache.GetValue(this, ComputeElementTypeFunc, null);

            const char Slash = '/';
            if (bindingPath.Length == 0 || bindingPath[bindingPath.Length - 1] == Slash)
            {
                // if the path did not actually contain a navigation property, then treat it as an unresolved path.
                // TODO: improve the error given in this case?
                return new UnresolvedNavigationPropertyPath(definingType, bindingPath, binding.Location);
            }

            IEdmNavigationProperty navigationProperty;
            string[] pathSegements = bindingPath.Split(Slash);
            for (int index = 0; index < pathSegements.Length - 1; index++)
            {
                string pathSegement = pathSegements[index];
                if (pathSegement.Length == 0)
                {
                    // TODO: improve the error given in this case?
                    return new UnresolvedNavigationPropertyPath(definingType, bindingPath, binding.Location);
                }

                IEdmEntityType derivedType = this.container.Context.FindType(pathSegement) as IEdmEntityType;
                if (derivedType == null)
                {
                    IEdmProperty property = definingType.FindProperty(pathSegement);
                    navigationProperty = property as IEdmNavigationProperty;
                    if (navigationProperty == null)
                    {
                        return new UnresolvedNavigationPropertyPath(definingType, bindingPath, binding.Location);
                    }

                    definingType = navigationProperty.ToEntityType();
                }
                else
                {
                    definingType = derivedType;
                }
            }

            navigationProperty = definingType.FindProperty(pathSegements.Last()) as IEdmNavigationProperty;
            if (navigationProperty == null)
            {
                // TODO: improve the error given in this case?
                navigationProperty = new UnresolvedNavigationPropertyPath(definingType, bindingPath, binding.Location);
            }

            return navigationProperty;
        }