src/Shared/EntityFramework/Internal/EntityEntries/PropertyEntryMetadata.cs
// <summary>
// Validates that the given name is a property of the declaring type (either on the CLR type or in the EDM)
// and that it is a complex or scalar property rather than a nav property and then returns metadata about
// the property.
// </summary>
// <param name="internalContext"> The internal context. </param>
// <param name="declaringType"> The type that the property is declared on. </param>
// <param name="requestedType"> The type of property requested, which may be 'object' if any type can be accepted. </param>
// <param name="propertyName"> Name of the property. </param>
// <returns> Metadata about the property, or null if the property does not exist or is a navigation property. </returns>
Validates that the given name is a property of the declaring type (either on the CLR type or in the EDM)
and that it is a complex or scalar property rather than a nav property and then returns metadata about
the property.
Validates that the given name is a property of the declaring type ( either on the CLR type or in the EDM ) and that it is a complex or scalar property rather than a nav property and then returns metadata about the property .
public static PropertyEntryMetadata ValidateNameAndGetMetadata(
            InternalContext internalContext, Type declaringType, Type requestedType, string propertyName)
        {
            DebugCheck.NotNull(internalContext);
            DebugCheck.NotNull(declaringType);
            DebugCheck.NotNull(requestedType);
            DebugCheck.NotEmpty(propertyName);
            Type propertyType;
            DbHelpers.GetPropertyTypes(declaringType).TryGetValue(propertyName, out propertyType);
            var metadataWorkspace = internalContext.ObjectContext.MetadataWorkspace;
            var edmType = metadataWorkspace.GetItem<StructuralType>(declaringType.FullNameWithNesting(), DataSpace.OSpace);
            var isMapped = false;
            var isComplex = false;
            EdmMember member;
            edmType.Members.TryGetValue(propertyName, false, out member);
            if (member != null)
            {
                var edmProperty = member as EdmProperty;
                if (edmProperty == null)
                {
                    return null;
                }
                if (propertyType == null)
                {
                    var asPrimitive = edmProperty.TypeUsage.EdmType as PrimitiveType;
                    if (asPrimitive != null)
                    {
                        propertyType = asPrimitive.ClrEquivalentType;
                    }
                    else
                    {
                        Debug.Assert(
                            edmProperty.TypeUsage.EdmType is StructuralType, "Expected a structural type if property type is not primitive.");
                        var objectItemCollection =
                            (ObjectItemCollection)metadataWorkspace.GetItemCollection(DataSpace.OSpace);
                        propertyType = objectItemCollection.GetClrType((StructuralType)edmProperty.TypeUsage.EdmType);
                    }
                }
                isMapped = true;
                isComplex = edmProperty.TypeUsage.EdmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType;
            }
            else
            {
                var propertyGetters = DbHelpers.GetPropertyGetters(declaringType);
                var propertySetters = DbHelpers.GetPropertySetters(declaringType);
                if (!(propertyGetters.ContainsKey(propertyName) || propertySetters.ContainsKey(propertyName)))
                {
                    return null;
                }
                Debug.Assert(propertyType != null, "If the property has a getter or setter, then it must exist and have a type.");
            }
            if (!requestedType.IsAssignableFrom(propertyType))
            {
                throw Error.DbEntityEntry_WrongGenericForProp(
                    propertyName, declaringType.Name, requestedType.Name, propertyType.Name);
            }
            return new PropertyEntryMetadata(declaringType, propertyType, propertyName, isMapped, isComplex);
        }
public static PropertyEntryMetadata ValidateNameAndGetMetadata ( InternalContext internalContext , Type declaringType , Type requestedType , string propertyName ) { DebugCheck . NotNull ( internalContext ) ; DebugCheck . NotNull ( declaringType ) ; DebugCheck . NotNull ( requestedType ) ; DebugCheck . NotEmpty ( propertyName ) ; Type propertyType ; DbHelpers . GetPropertyTypes ( declaringType ) . TryGetValue ( propertyName , out propertyType ) ; var metadataWorkspace = internalContext . ObjectContext . MetadataWorkspace ; var edmType = metadataWorkspace . GetItem < StructuralType > ( declaringType . FullNameWithNesting ( ) , DataSpace . OSpace ) ; var isMapped = false ; var isComplex = false ; EdmMember member ; edmType . Members . TryGetValue ( propertyName , false , out member ) ; if ( member != null ) { var edmProperty = member as EdmProperty ; if ( edmProperty == null ) { return null ; } if ( propertyType == null ) { var asPrimitive = edmProperty . TypeUsage . EdmType as PrimitiveType ; if ( asPrimitive != null ) { propertyType = asPrimitive . ClrEquivalentType ; } else { Debug . Assert ( edmProperty . TypeUsage . EdmType is StructuralType , " " ) ; var objectItemCollection = ( ObjectItemCollection ) metadataWorkspace . GetItemCollection ( DataSpace . OSpace ) ; propertyType = objectItemCollection . GetClrType ( ( StructuralType ) edmProperty . TypeUsage . EdmType ) ; } } isMapped = true ; isComplex = edmProperty . TypeUsage . EdmType . BuiltInTypeKind == BuiltInTypeKind . ComplexType ; } else { var propertyGetters = DbHelpers . GetPropertyGetters ( declaringType ) ; var propertySetters = DbHelpers . GetPropertySetters ( declaringType ) ; if ( ! ( propertyGetters . ContainsKey ( propertyName ) || propertySetters . ContainsKey ( propertyName ) ) ) { return null ; } Debug . Assert ( propertyType != null , " " ) ; } if ( ! requestedType . IsAssignableFrom ( propertyType ) ) { throw Error . DbEntityEntry_WrongGenericForProp ( propertyName , declaringType . Name , requestedType . Name , propertyType . Name ) ; } return new PropertyEntryMetadata ( declaringType , propertyType , propertyName , isMapped , isComplex ) ; }
Validates that the given name is a property of the declaring type (either on the CLR type or in the EDM)
and that it is a complex or scalar property rather than a nav property and then returns metadata about
the property.
Validates that the given name is a property of the declaring type ( either on the CLR type or in the EDM ) and that it is a complex or scalar property rather than a nav property and then returns metadata about the property .
// If the property is in the model, then it must be a scalar or complex property, not a nav prop // If the prop is not in the model, then it must have a getter or a setter
Metadata about the property, or null if the property does not exist or is a navigation property.
Metadata about the property or null if the property does not exist or is a navigation property .
internalContext
InternalContext
The internal context.
The internal context .
declaringType
Type
The type that the property is declared on.
The type that the property is declared on .
requestedType
Type
The type of property requested, which may be 'object' if any type can be accepted.
The type of property requested which may be ' object ' if any type can be accepted .
propertyName
string
Name of the property.
Name of the property .