public static bool IsAssignableFrom(this IEdmEntityType baseType, IEdmEntityType subtype) { Utils.CheckArgumentNull(baseType, nameof(baseType)); Utils.CheckArgumentNull(subtype, nameof(subtype)); if (baseType.TypeKind != subtype.TypeKind) { return(false); } if (subtype.IsEquivalentTo(baseType)) { return(true); } IEdmStructuredType structuredSubType = subtype; while (structuredSubType != null) { if (structuredSubType.IsEquivalentTo(baseType)) { return(true); } structuredSubType = structuredSubType.BaseType; } return(false); }
/// <summary> /// Constructs an instance of <see cref="ODataQueryContext"/> with EdmModel, Entity's CLR type and the corresponding /// EntitySet stored in the model. If the given EntitySet is not in the model, this constructor will throw. /// </summary> /// <param name="model">The EdmModel that includes the Entity and EntitySet information.</param> /// <param name="entityClrType">The entity's CLR type information.</param> /// <param name="entitySet">The corresponding EntitySet stored in the model.</param> public ODataQueryContext(IEdmModel model, Type entityClrType, IEdmEntitySet entitySet) { if (model == null) { throw Error.ArgumentNull("model"); } if (entityClrType == null) { throw Error.ArgumentNull("entityClrType"); } if (entitySet == null) { throw Error.ArgumentNull("entitySet"); } // check if the model contains the entitySet IEnumerable <IEdmEntityContainer> containers = model.EntityContainers(); if (containers != null) { IEdmEntityContainer singleContainer = containers.Single(); if (singleContainer != null) { IEnumerable <IEdmEntitySet> entitySets = singleContainer.EntitySets(); if (!entitySets.Contains(entitySet)) { throw Error.Argument(parameterName: "entitySet", messageFormat: SRResources.EntitySetMustBeInTheModel); } } } // Check if the model contains the entityClrType IEdmEntityType edmType = model.GetEdmType(entityClrType) as IEdmEntityType; if (edmType == null) { throw Error.Argument("entityClrType", SRResources.EntityClrTypeNotFound, entityClrType.FullName); } // Check if the entitySetName matches the entityClrType if (!edmType.IsEquivalentTo(entitySet.ElementType)) { throw Error.Argument("entityClrType", SRResources.EntityClrTypeNotMatchEntitySet, entityClrType.FullName, entitySet.Name); } // Now we can set everything Model = model; EntityClrType = entityClrType; EntitySet = entitySet; }
/// <summary> /// Constructs an instance of <see cref="ODataQueryContext"/> with EdmModel, Entity's CLR type and the name /// of EntitySet stored in the model. /// </summary> /// <param name="model">The EdmModel that includes the Entity and EntitySet information.</param> /// <param name="entityClrType">The entity's CLR type information.</param> /// <param name="entitySetName">The name of EntitySet stored in the model.</param> public ODataQueryContext(IEdmModel model, Type entityClrType, string entitySetName) { if (model == null) { throw Error.ArgumentNull("model"); } if (entityClrType == null) { throw Error.ArgumentNull("entityClrType"); } if (String.IsNullOrEmpty(entitySetName)) { throw Error.ArgumentNullOrEmpty("entitySetName"); } // check if we can successfully retrieve an entitySet from the model with the given entitySetName IEnumerable <IEdmEntityContainer> containers = model.EntityContainers(); foreach (IEdmEntityContainer container in containers) { EntitySet = container.FindEntitySet(entitySetName); if (EntitySet != null) { break; } } if (EntitySet == null) { throw Error.Argument("entitySetName", SRResources.EntitySetNotFoundForName, entitySetName); } // Check if the model contains the entityClrType IEdmEntityType edmType = model.GetEdmType(entityClrType) as IEdmEntityType; if (edmType == null) { throw Error.Argument("entityClrType", SRResources.EntityClrTypeNotFound, entityClrType.FullName); } // Check if the entitySetName matches the entityClrType if (!edmType.IsEquivalentTo(EntitySet.ElementType)) { throw Error.Argument("entityClrType", SRResources.EntityClrTypeNotMatchEntitySetName, entityClrType.FullName, entitySetName); } Model = model; EntityClrType = entityClrType; }
/// <summary> /// Returns the fully qualified name of <paramref name="entityType"/> if it is a derived type of the <paramref name="entitySet"/>; /// returns null if <paramref name="entityType"/> is the root type of <paramref name="entitySet"/>. /// </summary> /// <param name="entitySet">The entity set in question.</param> /// <param name="entityType">The eneity type in question.</param> /// <returns> /// Returns the fully qualified name of <paramref name="entityType"/> if it is a derived type of the <paramref name="entitySet"/>; /// returns null if <paramref name="entityType"/> is the root type of <paramref name="entitySet"/>. /// </returns> private static string GetTypecast(IEdmEntitySet entitySet, IEdmEntityType entityType) { DebugUtils.CheckNoExternalCallers(); if (entitySet == null || entityType == null) { return(null); } // The client type resolver is not necessary for writes, ResolveEntitySetElementType() will return entitySet.ElementType when model is null // and we don't need to look up the type in the model. IEdmEntityType entitySetElementType = EdmTypeWriterResolver.Instance.GetElementType(entitySet); if (entitySetElementType.IsEquivalentTo(entityType)) { return(null); } if (!entitySetElementType.IsAssignableFrom(entityType)) { throw new ODataException(OData.Strings.ODataJsonLightMetadataUriBuilder_ValidateDerivedType(entitySetElementType.FullName(), entityType.FullName())); } return(entityType.ODataFullName()); }