private IEdmCollectionType EnsureCollectionItemTypeIsPrimitiveOrComplex(ResourceType itemResourceType, List <KeyValuePair <string, object> > customAnnotations) { IEdmCollectionType type; if (!this.primitiveOrComplexCollectionTypeCache.TryGetValue(itemResourceType, out type)) { IEdmTypeReference reference; switch (itemResourceType.ResourceTypeKind) { case ResourceTypeKind.ComplexType: reference = this.EnsureTypeReference(itemResourceType, customAnnotations); reference = reference.IsNullable ? reference.Clone(false) : reference; break; case ResourceTypeKind.Primitive: { MetadataProviderUtils.GetAndRemoveNullableFacet(customAnnotations); IEdmPrimitiveTypeReference typeReference = MetadataProviderUtils.CreatePrimitiveTypeReference(itemResourceType, customAnnotations); reference = typeReference.IsNullable ? typeReference.Clone(false) : typeReference; break; } default: throw new InvalidOperationException(System.Data.Services.Strings.MetadataProviderEdmModel_UnsupportedCollectionItemType_PrimitiveOrComplex(itemResourceType.ResourceTypeKind.ToString())); } type = new EdmCollectionType(reference); this.primitiveOrComplexCollectionTypeCache.Add(itemResourceType, type); } return(type); }
/// <summary> /// Converts a primitive data type to type. /// </summary> /// <param name="primitiveType">The primitive data type to convert.</param> /// <returns>The corresponding primitive type.</returns> private static IEdmPrimitiveTypeReference GetPrimitiveTypeReference(PrimitiveDataType primitiveType) { Debug.Assert(primitiveType != null, "primitiveType != null"); Type systemType = EntityModelUtils.GetPrimitiveClrType(primitiveType); // NOTE: if the primitiveType is not nullable but the type reference constructed from the CLR type is, // adjust the nullability if necessary. IEdmPrimitiveTypeReference primitiveTypeReference = MetadataUtils.GetPrimitiveTypeReference(systemType); if (primitiveType.IsNullable != primitiveTypeReference.IsNullable) { primitiveTypeReference = (IEdmPrimitiveTypeReference)primitiveTypeReference.Clone(primitiveType.IsNullable); } return(primitiveTypeReference); }
private IEdmProperty CreateProperty(EdmStructuredType declaringType, ResourceProperty resourceProperty) { IEdmProperty property; List <KeyValuePair <string, object> > annotations = (resourceProperty.CustomAnnotations == null) ? null : resourceProperty.CustomAnnotations.ToList <KeyValuePair <string, object> >(); ODataNullValueBehaviorKind nullValueReadBehaviorKind = ODataNullValueBehaviorKind.Default; if (resourceProperty.IsOfKind(ResourcePropertyKind.Primitive) || resourceProperty.IsOfKind(ResourcePropertyKind.Stream)) { IEdmPrimitiveTypeReference typeReference = MetadataProviderUtils.CreatePrimitiveTypeReference(resourceProperty.ResourceType, annotations); if (resourceProperty.IsOfKind(ResourcePropertyKind.Key)) { if (typeReference.IsNullable) { typeReference = (IEdmPrimitiveTypeReference)typeReference.Clone(false); } nullValueReadBehaviorKind = ODataNullValueBehaviorKind.IgnoreValue; } else if (MetadataProviderUtils.ShouldDisablePrimitivePropertyNullValidation(resourceProperty, typeReference)) { nullValueReadBehaviorKind = ODataNullValueBehaviorKind.DisableValidation; } string andRemoveDefaultValue = MetadataProviderUtils.GetAndRemoveDefaultValue(annotations); EdmConcurrencyMode concurrencyMode = resourceProperty.IsOfKind(ResourcePropertyKind.ETag) ? EdmConcurrencyMode.Fixed : EdmConcurrencyMode.None; property = declaringType.AddStructuralProperty(resourceProperty.Name, typeReference, andRemoveDefaultValue, concurrencyMode); string mimeType = resourceProperty.MimeType; if (!string.IsNullOrEmpty(mimeType)) { this.SetMimeType(property, mimeType); } } else if (resourceProperty.IsOfKind(ResourcePropertyKind.ComplexType)) { IEdmTypeReference reference2 = this.EnsureTypeReference(resourceProperty.ResourceType, annotations); string defaultValue = MetadataProviderUtils.GetAndRemoveDefaultValue(annotations); property = declaringType.AddStructuralProperty(resourceProperty.Name, reference2, defaultValue, EdmConcurrencyMode.None); if (this.metadataProvider.IsV1Provider && !reference2.IsNullable) { nullValueReadBehaviorKind = ODataNullValueBehaviorKind.DisableValidation; } } else if (resourceProperty.IsOfKind(ResourcePropertyKind.Collection)) { string str4 = MetadataProviderUtils.GetAndRemoveDefaultValue(annotations); IEdmTypeReference reference3 = this.EnsureTypeReference(resourceProperty.ResourceType, annotations); property = declaringType.AddStructuralProperty(resourceProperty.Name, reference3, str4, EdmConcurrencyMode.None); } else { if (!resourceProperty.IsOfKind(ResourcePropertyKind.ResourceSetReference) && !resourceProperty.IsOfKind(ResourcePropertyKind.ResourceReference)) { throw new InvalidOperationException(System.Data.Services.Strings.MetadataProviderEdmModel_UnsupportedResourcePropertyKind(resourceProperty.Kind.ToString())); } EdmEntityType type = (EdmEntityType)declaringType; IEdmTypeReference reference4 = resourceProperty.IsOfKind(ResourcePropertyKind.ResourceSetReference) ? this.EnsureEntityPrimitiveOrComplexCollectionTypeReference(resourceProperty.ResourceType, annotations) : this.EnsureTypeReference(resourceProperty.ResourceType, annotations); property = new MetadataProviderEdmNavigationProperty(type, resourceProperty.Name, reference4); type.AddProperty(property); } this.SetNullValueReaderBehavior(property, nullValueReadBehaviorKind); MetadataProviderUtils.ConvertCustomAnnotations(this, annotations, property); return(property); }