Ejemplo n.º 1
0
        public static object GetKeyFor <TEntity>(this IContainerMetadata containerMetadata, TEntity entity)
        {
            Contract.Requires <ArgumentNullException>(containerMetadata != null);
            Contract.Requires <ArgumentNullException>(entity != null);

            IEntityTypeMetadata entityTypeMetadata = containerMetadata.GetEntityType(typeof(TEntity));

            return(entityTypeMetadata.EntityKeyFunction(entity));
        }
Ejemplo n.º 2
0
        private static void ExpandGenericNavigationActionsForAllNavigationProperties(IEnumerable <HttpActionDescriptor> navigationActions,
                                                                                     List <HttpActionDescriptor> expandedDescriptors,
                                                                                     List <HttpActionDescriptor> removeDescriptors,
                                                                                     Func <string, string> actionNameBuilder)
        {
            foreach (HttpActionDescriptor navigationAction in navigationActions)
            {
                ReflectedHttpActionDescriptor reflectedHttpActionDescriptor = navigationAction as ReflectedHttpActionDescriptor;
                if ((reflectedHttpActionDescriptor != null) &&
                    reflectedHttpActionDescriptor.MethodInfo.IsGenericMethodDefinition &&
                    reflectedHttpActionDescriptor.MethodInfo.GetGenericArguments().Length == 1)
                {
                    // Lookup the EntitySet metadata for the controller
                    IContainerMetadata containerMetadata = reflectedHttpActionDescriptor.ControllerDescriptor.GetContainerMetadata();
                    if (containerMetadata != null)
                    {
                        IEntitySetMetadata entitySetMetadata = containerMetadata.GetEntitySet(reflectedHttpActionDescriptor.ControllerDescriptor.ControllerName);
                        if (entitySetMetadata != null)
                        {
                            foreach (IEntityTypeMetadata entityTypeMetadata in entitySetMetadata.ElementTypeHierarchyMetadata)
                            {
                                // Foreach NavigationProperty in all of the entity types, add a new HttpActionDescriptor
                                foreach (var edmNavigationProperty in entityTypeMetadata.EdmType.DeclaredProperties.OfType <IEdmNavigationProperty>())
                                {
                                    IEdmEntityType      toEntityType         = edmNavigationProperty.ToEntityType();
                                    IEntityTypeMetadata propertyTypeMetadata = containerMetadata.GetEntityType(toEntityType);

                                    Type       tProperty          = propertyTypeMetadata.ClrType;
                                    MethodInfo genericMethod      = reflectedHttpActionDescriptor.MethodInfo.MakeGenericMethod(tProperty);
                                    string     expandedActionName = actionNameBuilder(edmNavigationProperty.Name);
                                    expandedDescriptors.Add(new RenamedReflectedHttpActionDescriptor(reflectedHttpActionDescriptor.ControllerDescriptor,
                                                                                                     genericMethod,
                                                                                                     expandedActionName));
                                }
                            }
                        }
                    }

                    removeDescriptors.Add(reflectedHttpActionDescriptor);
                }
            }
        }