Ejemplo n.º 1
0
        private static Dictionary <Type, IEdmStructuredType> AddTypes(this EdmModel model, IEnumerable <StructuralTypeConfiguration> types)
        {
            StructuralTypeConfiguration[] configTypes = types.ToArray();

            // build types
            Dictionary <Type, IEdmStructuredType> edmTypes = EdmTypeBuilder.GetTypes(configTypes);

            // Add an annotate types
            model.AddTypes(edmTypes);
            model.AddClrTypeAnnotations(edmTypes);
            return(edmTypes);
        }
        private static Dictionary <string, IEdmStructuredType> AddTypes(this EdmModel model, IEnumerable <IStructuralTypeConfiguration> types)
        {
            IStructuralTypeConfiguration[] configTypes = types.ToArray();
            // build types
            IEdmStructuredType[] edmTypes = EdmTypeBuilder.GetTypes(configTypes).ToArray();
            // index types
            Dictionary <string, IEdmStructuredType> edmTypeMap = edmTypes.ToDictionary(t => (t as IEdmSchemaType).FullName());

            // Add an annotate types
            model.AddTypes(edmTypes);
            model.AddClrTypeAnnotations(GenerateEdmToClrMap(configTypes, edmTypeMap));
            return(edmTypeMap);
        }
        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);
        }