Ejemplo n.º 1
0
        private static MemberExpression[] GetKeyExpressions(ModelBuilder.OeEdmModelMetadataProvider metadataProvider, Type entityType)
        {
            var keyPropertyList = new List <PropertyInfo>();

            foreach (PropertyInfo property in entityType.GetProperties())
            {
                if (metadataProvider.IsKey(property))
                {
                    keyPropertyList.Add(property);
                }
            }

            if (keyPropertyList.Count == 0)
            {
                PropertyInfo keyProperty = entityType.GetPropertyIgnoreCase("Id");
                if (keyProperty == null)
                {
                    throw new InvalidOperationException("Key not found in " + entityType.Name);
                }

                keyPropertyList.Add(keyProperty);
            }
            PropertyInfo[] keyProperties = keyPropertyList.ToArray();
            metadataProvider.SortClrPropertyByOrder(keyProperties);

            var keyExpressions            = new MemberExpression[keyProperties.Length];
            ParameterExpression parameter = Expression.Parameter(entityType);

            for (int i = 0; i < keyProperties.Length; i++)
            {
                keyExpressions[i] = Expression.Property(parameter, keyProperties[i]);
            }
            return(keyExpressions);
        }
Ejemplo n.º 2
0
        private FieldType CreateStructuralFieldType(PropertyInfo propertyInfo)
        {
            Type graphType;
            bool isNullable = !_modelMetadataProvider.IsRequired(propertyInfo);
            Type enumType   = propertyInfo.PropertyType;

            if (enumType.IsEnum || ((enumType = Nullable.GetUnderlyingType(enumType)) != null && enumType.IsEnum))
            {
                graphType = typeof(EnumerationGraphType <>).MakeGenericType(enumType);
                if (!isNullable)
                {
                    graphType = typeof(NonNullGraphType <>).MakeGenericType(graphType);
                }
            }
            else
            {
                if (_modelMetadataProvider.IsKey(propertyInfo))
                {
                    graphType = typeof(IdGraphType);
                }
                else
                if (propertyInfo.PropertyType == typeof(DateTimeOffset) || propertyInfo.PropertyType == typeof(DateTimeOffset?))
                {
                    graphType = typeof(DateTime).GetGraphTypeFromType(isNullable);
                }
                else
                {
                    graphType = propertyInfo.PropertyType.GetGraphTypeFromType(isNullable);
                }
            }

            var fieldType = new FieldType()
            {
                Name     = propertyInfo.Name,
                Type     = graphType,
                Resolver = new FieldResolver(propertyInfo),
            };

            return(fieldType);
        }