private static string GetClrTypeName(MetadataWorkspace metadataWorkspace, StructuralType cSpaceType)
        {
            if (cSpaceType != null)
            {
                StructuralType oSpaceType;

                if (metadataWorkspace.TryGetObjectSpaceType(cSpaceType, out oSpaceType))
                {
                    // interesting note: oSpaceType is of type ClrType - an internal EF type that contains a EdmType to CLR type mapping...
                    // so instead of getting the type name, we could go straight for the type
                    // by doing: oSpaceType.GetProperty("ClrType",BindingFlags.Instance|BindingFlags.NonPublic).GetValue(oSpaceType, null);
                    // but the classes are internal, so they might change and I don't want to touch them directly...
                    return(oSpaceType.FullName);
                }
            }

            return(null);
        }
        private static void ValidateTypeOfKeyValue(
            MetadataWorkspace workspace,
            EdmMember keyMember,
            object keyValue,
            bool isArgumentException,
            string argumentName)
        {
            EdmType edmType = keyMember.TypeUsage.EdmType;

            if (Helper.IsPrimitiveType(edmType))
            {
                Type clrEquivalentType = ((PrimitiveType)edmType).ClrEquivalentType;
                if (!(clrEquivalentType != keyValue.GetType()))
                {
                    return;
                }
                if (isArgumentException)
                {
                    throw new ArgumentException(Strings.EntityKey_IncorrectValueType((object)keyMember.Name, (object)clrEquivalentType.FullName, (object)keyValue.GetType().FullName), argumentName);
                }
                throw new InvalidOperationException(Strings.EntityKey_IncorrectValueType((object)keyMember.Name, (object)clrEquivalentType.FullName, (object)keyValue.GetType().FullName));
            }
            EnumType objectSpaceType;

            if (workspace.TryGetObjectSpaceType((EnumType)edmType, out objectSpaceType))
            {
                Type clrType = objectSpaceType.ClrType;
                if (!(clrType != keyValue.GetType()))
                {
                    return;
                }
                if (isArgumentException)
                {
                    throw new ArgumentException(Strings.EntityKey_IncorrectValueType((object)keyMember.Name, (object)clrType.FullName, (object)keyValue.GetType().FullName), argumentName);
                }
                throw new InvalidOperationException(Strings.EntityKey_IncorrectValueType((object)keyMember.Name, (object)clrType.FullName, (object)keyValue.GetType().FullName));
            }
            if (isArgumentException)
            {
                throw new ArgumentException(Strings.EntityKey_NoCorrespondingOSpaceTypeForEnumKeyMember((object)keyMember.Name, (object)edmType.FullName), argumentName);
            }
            throw new InvalidOperationException(Strings.EntityKey_NoCorrespondingOSpaceTypeForEnumKeyMember((object)keyMember.Name, (object)edmType.FullName));
        }
        /// <summary>Gets the CLR type mapped to the specified C-Space type.</summary>
        /// <param name="workspace">Workspace in which the type is defined.</param>
        /// <param name="edmType">C-Space type whose matching clr type needs to be looked up.</param>
        /// <returns>The resolved <see cref="Type"/> for the given <paramref name="edmType"/>.</returns>
        internal static Type GetClrTypeForCSpaceType(MetadataWorkspace workspace, StructuralType edmType)
        {
            Debug.Assert(workspace != null, "workspace != null");
            Debug.Assert(edmType != null, "edmType != null");
            Debug.Assert(
               edmType.BuiltInTypeKind == BuiltInTypeKind.EntityType || edmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType,
               "Must be entityType or complexType");

            StructuralType ospaceType;
            if (workspace.TryGetObjectSpaceType(edmType, out ospaceType))
            {
                ObjectItemCollection objectItemCollection = (ObjectItemCollection)workspace.GetItemCollection(DataSpace.OSpace);
                return objectItemCollection.GetClrType(ospaceType);
            }

            return null;
        }