public NavigationPropertyBinding AddBinding(NavigationPropertyConfiguration navigationConfiguration, IEntitySetConfiguration targetEntitySet)
        {
            NavigationPropertyBinding navigationPropertyBinding = null;

            if (_entitySetBindings.ContainsKey(navigationConfiguration))
            {
                navigationPropertyBinding = _entitySetBindings[navigationConfiguration];
                if (navigationPropertyBinding.EntitySet != targetEntitySet)
                {
                    throw Error.NotSupported(SRResources.RebindingNotSupported);
                }
            }
            else
            {
                navigationPropertyBinding = new NavigationPropertyBinding(navigationConfiguration, targetEntitySet);
                _entitySetBindings[navigationConfiguration] = navigationPropertyBinding;
            }
            return(navigationPropertyBinding);
        }
        private static Dictionary <string, EdmEntitySet> AddEntitySets(this EdmModel model, IEnumerable <IEntitySetConfiguration> configurations, EdmEntityContainer container, Dictionary <string, IEdmStructuredType> edmTypeMap)
        {
            // build the entitysets and their annotations
            IEnumerable <Tuple <EdmEntitySet, IEntitySetConfiguration> > entitySets = AddEntitySets(configurations, container, edmTypeMap);
            var entitySetAndAnnotations = entitySets.Select(e => new
            {
                EntitySet     = e.Item1,
                Configuration = e.Item2,
                Annotations   = new
                {
                    LinkBuilder = new EntitySetLinkBuilderAnnotation(e.Item2),
                    Url         = new EntitySetUrlAnnotation {
                        Url = e.Item2.GetUrl()
                    }
                }
            }).ToArray();

            // index the entitySets by name
            Dictionary <string, EdmEntitySet> edmEntitySetMap = entitySetAndAnnotations.ToDictionary(e => e.EntitySet.Name, e => e.EntitySet);

            // apply the annotations
            foreach (var iter in entitySetAndAnnotations)
            {
                EdmEntitySet            entitySet     = iter.EntitySet;
                IEntitySetConfiguration configuration = iter.Configuration;
                model.SetAnnotationValue <EntitySetUrlAnnotation>(entitySet, iter.Annotations.Url);
                model.SetAnnotationValue <IEntitySetLinkBuilder>(entitySet, iter.Annotations.LinkBuilder);

                foreach (NavigationPropertyConfiguration navigation in configuration.EntityType.NavigationProperties)
                {
                    NavigationPropertyBinding binding = configuration.FindBinding(navigation);
                    if (binding != null)
                    {
                        EdmEntityType          edmEntityType         = edmTypeMap[configuration.EntityType.FullName] as EdmEntityType;
                        IEdmNavigationProperty edmNavigationProperty = edmEntityType.NavigationProperties().Single(np => np.Name == navigation.Name);

                        entitySet.AddNavigationTarget(edmNavigationProperty, edmEntitySetMap[binding.EntitySet.Name]);
                        iter.Annotations.LinkBuilder.AddNavigationPropertyLinkBuilder(edmNavigationProperty, configuration.GetNavigationPropertyLink(edmNavigationProperty.Name));
                    }
                }
            }
            return(edmEntitySetMap);
        }
        public NavigationPropertyBinding AddBinding(NavigationPropertyConfiguration navigationConfiguration, IEntitySetConfiguration targetEntitySet)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

            if (targetEntitySet == null)
            {
                throw Error.ArgumentNull("targetEntitySet");
            }

            IEntityTypeConfiguration declaringEntityType = navigationConfiguration.DeclaringEntityType;

            if (!(declaringEntityType.IsAssignableFrom(EntityType) || EntityType.IsAssignableFrom(declaringEntityType)))
            {
                throw Error.InvalidOperation(SRResources.NavigationPropertyNotInHierarchy, declaringEntityType.FullName, EntityType.FullName, Name);
            }

            NavigationPropertyBinding navigationPropertyBinding = null;

            if (_entitySetBindings.ContainsKey(navigationConfiguration))
            {
                navigationPropertyBinding = _entitySetBindings[navigationConfiguration];
                if (navigationPropertyBinding.EntitySet != targetEntitySet)
                {
                    throw Error.NotSupported(SRResources.RebindingNotSupported);
                }
            }
            else
            {
                navigationPropertyBinding = new NavigationPropertyBinding(navigationConfiguration, targetEntitySet);
                _entitySetBindings[navigationConfiguration] = navigationPropertyBinding;
            }
            return(navigationPropertyBinding);
        }
Example #4
0
        private static void AddNavigationBindings(EntitySetConfiguration configuration, EdmEntitySet entitySet, EntitySetLinkBuilderAnnotation linkBuilder, ODataModelBuilder builder,
                                                  Dictionary <string, IEdmStructuredType> edmTypeMap, Dictionary <string, EdmEntitySet> edmEntitySetMap)
        {
            foreach (EntityTypeConfiguration entity in builder.ThisAndBaseAndDerivedTypes(configuration.EntityType))
            {
                foreach (NavigationPropertyConfiguration navigation in entity.NavigationProperties)
                {
                    NavigationPropertyBinding binding = configuration.FindBinding(navigation);
                    if (binding != null)
                    {
                        EdmEntityType          edmEntityType         = edmTypeMap[entity.FullName] as EdmEntityType;
                        IEdmNavigationProperty edmNavigationProperty = edmEntityType.NavigationProperties().Single(np => np.Name == navigation.Name);

                        entitySet.AddNavigationTarget(edmNavigationProperty, edmEntitySetMap[binding.EntitySet.Name]);

                        Func <EntityInstanceContext, IEdmNavigationProperty, Uri> linkBuilderFunc = configuration.GetNavigationPropertyLink(navigation);
                        if (linkBuilderFunc != null)
                        {
                            linkBuilder.AddNavigationPropertyLinkBuilder(edmNavigationProperty, linkBuilderFunc);
                        }
                    }
                }
            }
        }
 public NavigationPropertyBinding AddBinding(NavigationPropertyConfiguration navigationConfiguration, IEntitySetConfiguration targetEntitySet)
 {
     NavigationPropertyBinding navigationPropertyBinding = null;
     if (_entitySetBindings.ContainsKey(navigationConfiguration))
     {
         navigationPropertyBinding = _entitySetBindings[navigationConfiguration];
         if (navigationPropertyBinding.EntitySet != targetEntitySet)
         {
             throw Error.NotSupported(SRResources.RebindingNotSupported);
         }
     }
     else
     {
         navigationPropertyBinding = new NavigationPropertyBinding(navigationConfiguration, targetEntitySet);
         _entitySetBindings[navigationConfiguration] = navigationPropertyBinding;
     }
     return navigationPropertyBinding;
 }
        /// <summary>
        /// Binds the given navigation property to the target entity set.
        /// </summary>
        /// <param name="navigationConfiguration">The navigation property.</param>
        /// <param name="targetEntitySet">The target entity set.</param>
        /// <returns>The <see cref="NavigationPropertyBinding"/> so that it can be further configured.</returns>
        public virtual NavigationPropertyBinding AddBinding(NavigationPropertyConfiguration navigationConfiguration, EntitySetConfiguration targetEntitySet)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

            if (targetEntitySet == null)
            {
                throw Error.ArgumentNull("targetEntitySet");
            }

            EntityTypeConfiguration declaringEntityType = navigationConfiguration.DeclaringEntityType;
            if (!(declaringEntityType.IsAssignableFrom(EntityType) || EntityType.IsAssignableFrom(declaringEntityType)))
            {
                throw Error.InvalidOperation(SRResources.NavigationPropertyNotInHierarchy, declaringEntityType.FullName, EntityType.FullName, Name);
            }

            NavigationPropertyBinding navigationPropertyBinding = null;
            if (_entitySetBindings.ContainsKey(navigationConfiguration))
            {
                navigationPropertyBinding = _entitySetBindings[navigationConfiguration];
                if (navigationPropertyBinding.EntitySet != targetEntitySet)
                {
                    throw Error.NotSupported(SRResources.RebindingNotSupported);
                }
            }
            else
            {
                navigationPropertyBinding = new NavigationPropertyBinding(navigationConfiguration, targetEntitySet);
                _entitySetBindings[navigationConfiguration] = navigationPropertyBinding;
            }
            return navigationPropertyBinding;
        }
        public static IEdmModel BuildEdmModel(string containerNamespace, string containerName, IEnumerable <IStructuralTypeConfiguration> entityTypeConfigurations, IEnumerable <IEntitySetConfiguration> entitySetConfigurations)
        {
            if (entityTypeConfigurations == null)
            {
                throw Error.ArgumentNull("entityTypeConfigurations");
            }

            if (entitySetConfigurations == null)
            {
                throw Error.ArgumentNull("entitySetConfigurations");
            }

            EdmModel model = new EdmModel();

            Dictionary <string, IEdmStructuredType> types = EdmTypeBuilder.GetTypes(entityTypeConfigurations)
                                                            .OfType <IEdmStructuredType>()
                                                            .ToDictionary(t => t.ToString());

            foreach (IEdmStructuredType type in types.Values)
            {
                if (type.TypeKind == EdmTypeKind.Complex)
                {
                    model.AddElement(type as IEdmComplexType);
                }
                else if (type.TypeKind == EdmTypeKind.Entity)
                {
                    model.AddElement(type as IEdmEntityType);
                }
                else
                {
                    throw Error.InvalidOperation(SRResources.UnsupportedEntityTypeInModel);
                }
            }

            if (entitySetConfigurations.Any())
            {
                EdmEntityContainer container = new EdmEntityContainer(containerNamespace, containerName);
                Dictionary <string, EdmEntitySet> edmEntitySetMap = new Dictionary <string, EdmEntitySet>();
                foreach (IEntitySetConfiguration entitySet in entitySetConfigurations)
                {
                    EdmEntitySet edmEntitySet = container.AddEntitySet(entitySet.Name, (IEdmEntityType)types[entitySet.EntityType.FullName]);
                    EntitySetLinkBuilderAnnotation entitySetLinkBuilderAnnotation = new EntitySetLinkBuilderAnnotation(entitySet);

                    model.SetEntitySetLinkBuilderAnnotation(edmEntitySet, entitySetLinkBuilderAnnotation);
                    model.SetAnnotationValue <EntitySetUrlAnnotation>(edmEntitySet, new EntitySetUrlAnnotation {
                        Url = entitySet.GetUrl()
                    });
                    edmEntitySetMap.Add(edmEntitySet.Name, edmEntitySet);
                }

                foreach (IEntitySetConfiguration entitySet in entitySetConfigurations)
                {
                    EdmEntitySet edmEntitySet = edmEntitySetMap[entitySet.Name];
                    EntitySetLinkBuilderAnnotation entitySetLinkBuilderAnnotation = model.GetEntitySetLinkBuilder(edmEntitySet) as EntitySetLinkBuilderAnnotation;
                    foreach (NavigationPropertyConfiguration navigation in entitySet.EntityType.NavigationProperties)
                    {
                        NavigationPropertyBinding binding = entitySet.FindBinding(navigation);
                        if (binding != null)
                        {
                            EdmEntityType          edmEntityType         = types[entitySet.EntityType.FullName] as EdmEntityType;
                            IEdmNavigationProperty edmNavigationProperty = edmEntityType.NavigationProperties().Single(np => np.Name == navigation.Name);

                            edmEntitySet.AddNavigationTarget(edmNavigationProperty, edmEntitySetMap[binding.EntitySet.Name]);
                            entitySetLinkBuilderAnnotation.AddNavigationPropertyLinkBuilder(edmNavigationProperty, entitySet.GetNavigationPropertyLink(edmNavigationProperty.Name));
                        }
                    }
                }

                model.AddElement(container);
            }

            return(model);
        }