public static StructuralType GetEdmType(MetadataWorkspace workspace, Type clrType) { if (workspace == null) { throw new ArgumentNullException("workspace"); } if (clrType == null) { throw new ArgumentNullException("clrType"); } if (clrType.IsPrimitive || clrType == typeof(object)) { return(null); } EdmType edmType = null; do { if (!workspace.TryGetType(clrType.Name, clrType.Namespace, DataSpace.OSpace, out edmType)) { workspace.LoadFromAssembly(clrType.Assembly); workspace.TryGetType(clrType.Name, clrType.Namespace, DataSpace.OSpace, out edmType); } }while (edmType == null && (clrType = clrType.BaseType) != typeof(object) && clrType != null); StructuralType result = null; if (edmType != null && (edmType.BuiltInTypeKind == BuiltInTypeKind.EntityType || edmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType)) { workspace.TryGetEdmSpaceType((StructuralType)edmType, out result); } return(result); }
/// <summary> /// Retrieves the <see cref="System.Data.Entity.Core.Metadata.Edm.StructuralType"/> corresponding to the given CLR type (where the /// type is an entity or complex type). /// </summary> /// <remarks> /// If no mapping exists for <paramref name="clrType"/>, but one does exist for one of its base /// types, we will return the mapping for the base type. /// </remarks> /// <param name="workspace">The <see cref="System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace"/></param> /// <param name="clrType">The CLR type</param> /// <returns>The <see cref="System.Data.Entity.Core.Metadata.Edm.StructuralType"/> corresponding to that CLR type, or <c>null</c> if the Type /// is not mapped.</returns> public static StructuralType GetEdmType(MetadataWorkspace workspace, Type clrType) { if (workspace == null) { throw Error.ArgumentNull("workspace"); } if (clrType == null) { throw Error.ArgumentNull("clrType"); } if (clrType.IsPrimitive || clrType == typeof(object)) { // want to avoid loading searching system assemblies for // types we know aren't entity or complex types return(null); } // We first locate the EdmType in "OSpace", which matches the name and namespace of the CLR type EdmType edmType; do { if (workspace.TryGetType(clrType.Name, clrType.Namespace, DataSpace.OSpace, out edmType)) { continue; } // If EF could not find this type, it could be because it is not loaded into // its current workspace. In this case, we explicitly load the assembly containing // the CLR type and try again. workspace.LoadFromAssembly(clrType.Assembly); workspace.TryGetType(clrType.Name, clrType.Namespace, DataSpace.OSpace, out edmType); } while (edmType == null && (clrType = clrType.BaseType) != typeof(object) && clrType != null); // Next we locate the StructuralType from the EdmType. // This 2-step process is necessary when the types CLR namespace does not match Edm namespace. // Look at the EdmEntityTypeAttribute on the generated entity classes to see this Edm namespace. StructuralType structuralType = null; if (edmType != null && (edmType.BuiltInTypeKind == BuiltInTypeKind.EntityType || edmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType)) { workspace.TryGetEdmSpaceType((StructuralType)edmType, out structuralType); } return(structuralType); }
protected override EdmType VisitType(EdmType type) { EdmType retType = type; if (BuiltInTypeKind.RefType == type.BuiltInTypeKind) { RefType refType = (RefType)type; EntityType mappedEntityType = (EntityType)this.VisitType(refType.ElementType); if (!object.ReferenceEquals(refType.ElementType, mappedEntityType)) { retType = new RefType(mappedEntityType); } } else if (BuiltInTypeKind.CollectionType == type.BuiltInTypeKind) { CollectionType collectionType = (CollectionType)type; TypeUsage mappedElementType = this.VisitTypeUsage(collectionType.TypeUsage); if (!object.ReferenceEquals(collectionType.TypeUsage, mappedElementType)) { retType = new CollectionType(mappedElementType); } } else if (BuiltInTypeKind.RowType == type.BuiltInTypeKind) { RowType rowType = (RowType)type; List <KeyValuePair <string, TypeUsage> > mappedPropInfo = null; for (int idx = 0; idx < rowType.Properties.Count; idx++) { EdmProperty originalProp = rowType.Properties[idx]; TypeUsage mappedPropType = this.VisitTypeUsage(originalProp.TypeUsage); if (!object.ReferenceEquals(originalProp.TypeUsage, mappedPropType)) { if (mappedPropInfo == null) { mappedPropInfo = new List <KeyValuePair <string, TypeUsage> >( rowType.Properties.Select( prop => new KeyValuePair <string, TypeUsage>(prop.Name, prop.TypeUsage) )); } mappedPropInfo[idx] = new KeyValuePair <string, TypeUsage>(originalProp.Name, mappedPropType); } } if (mappedPropInfo != null) { IEnumerable <EdmProperty> mappedProps = mappedPropInfo.Select(propInfo => new EdmProperty(propInfo.Key, propInfo.Value)); retType = new RowType(mappedProps, rowType.InitializerMetadata); } } else { if (!_metadata.TryGetType(type.Name, type.NamespaceName, type.DataSpace, out retType) || null == retType) { throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Copier_TypeNotFound(TypeHelpers.GetFullName(type))); } } return(retType); }
/// <summary> /// Retrieves the <see cref="StructuralType"/> corresponding to the given CLR type (where the /// type is an entity or complex type). /// </summary> /// <remarks> /// If no mapping exists for <paramref name="clrType"/>, but one does exist for one of its base /// types, we will return the mapping for the base type. /// </remarks> /// <param name="workspace">The <see cref="MetadataWorkspace"/></param> /// <param name="clrType">The CLR type</param> /// <returns>The <see cref="StructuralType"/> corresponding to that CLR type, or <c>null</c> if the Type /// is not mapped.</returns> public static StructuralType GetEdmType(MetadataWorkspace workspace, Type clrType) { if (workspace == null) { throw new ArgumentNullException("workspace"); } if (clrType == null) { throw new ArgumentNullException("clrType"); } if (clrType.IsPrimitive || clrType == typeof(object)) { // want to avoid loading searching system assemblies for // types we know aren't entity or complex types return null; } // We first locate the EdmType in "OSpace", which matches the name and namespace of the CLR type EdmType edmType = null; do { if (!workspace.TryGetType(clrType.Name, clrType.Namespace, DataSpace.OSpace, out edmType)) { // If EF could not find this type, it could be because it is not loaded into // its current workspace. In this case, we explicitly load the assembly containing // the CLR type and try again. workspace.LoadFromAssembly(clrType.Assembly); workspace.TryGetType(clrType.Name, clrType.Namespace, DataSpace.OSpace, out edmType); } } while (edmType == null && (clrType = clrType.BaseType) != typeof(object) && clrType != null); // Next we locate the StructuralType from the EdmType. // This 2-step process is necessary when the types CLR namespace does not match Edm namespace. // Look at the EdmEntityTypeAttribute on the generated entity classes to see this Edm namespace. StructuralType structuralType = null; if (edmType != null && (edmType.BuiltInTypeKind == BuiltInTypeKind.EntityType || edmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType)) { workspace.TryGetEdmSpaceType((StructuralType)edmType, out structuralType); } return structuralType; }
private static IEnumerable <EdmProperty> GetKeyPropertyNames(Type type, MetadataWorkspace workspace) { EdmType edmType; if (!workspace.TryGetType(type.Name, type.Namespace, DataSpace.OSpace, out edmType)) { workspace.TryGetType(type.Name, type.Namespace, DataSpace.CSpace, out edmType); } if (edmType != null) { return(edmType.MetadataProperties.Where(mp => mp.Name == "KeyMembers") .SelectMany(mp => mp.Value as ReadOnlyMetadataCollection <EdmMember>) .OfType <EdmProperty>()); } return(null); }
private static EntityType GetEntityType(MetadataWorkspace metadataWorkspace, ObjectStateEntry entry) { var type = ObjectContext.GetObjectType(entry.Entity.GetType()); var entityNamespace = entry.EntitySet.ElementType.NamespaceName; EdmType edmType; if (metadataWorkspace.TryGetType(type.Name, entityNamespace, DataSpace.CSpace, out edmType)) { return(edmType as EntityType); } return(null); }
private static IEnumerable<string> GetKeyPropertyNames(Type type, MetadataWorkspace workspace) { EdmType edmType; if (workspace.TryGetType(type.Name, type.Namespace, DataSpace.OSpace, out edmType)) { return edmType.MetadataProperties.Where(mp => mp.Name == "KeyMembers") .SelectMany(mp => mp.Value as ReadOnlyMetadataCollection<EdmMember>) .OfType<EdmProperty>().Select(edmProperty => edmProperty.Name); } return null; }
private static IEnumerable<string> GetDeclaredPropertyNames(Type type, MetadataWorkspace workspace) { EdmType edmType; return workspace.TryGetType(type.Name, type.Namespace, DataSpace.OSpace, out edmType) ? ((System.Data.Entity.Core.Metadata.Edm.EntityType) edmType).DeclaredProperties.Select(x => x.Name) : null; }
private static IEnumerable <string> GetDeclaredPropertyNames(Type type, MetadataWorkspace workspace) { EdmType edmType; return(workspace.TryGetType(type.Name, type.Namespace, DataSpace.OSpace, out edmType) ? ((System.Data.Entity.Core.Metadata.Edm.EntityType)edmType).DeclaredProperties.Select(x => x.Name) : null); }