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); }
public static EntityType GetCSpacetype(Type currentType, MetadataWorkspace mdw) { mdw.LoadFromAssembly(currentType.Assembly); EntityType ospaceEntityType = null; StructuralType cspaceEntityType = null; if (mdw.TryGetItem <EntityType>( currentType.FullName, DataSpace.OSpace, out ospaceEntityType)) { if (mdw.TryGetEdmSpaceType(ospaceEntityType, out cspaceEntityType)) { return(cspaceEntityType as EntityType); } } return(null); }
/// <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; }
public static EntityType GetCSpacetype(Type currentType, MetadataWorkspace mdw) { mdw.LoadFromAssembly(currentType.Assembly); EntityType ospaceEntityType = null; StructuralType cspaceEntityType = null; if (mdw.TryGetItem<EntityType>( currentType.FullName, DataSpace.OSpace, out ospaceEntityType)) { if (mdw.TryGetEdmSpaceType(ospaceEntityType, out cspaceEntityType)) return cspaceEntityType as EntityType; } return null; }