internal static Type GetClrTypeFromCSpaceType(this MetadataWorkspace workspace, EdmType conceptualType) { var itemCollection = (ObjectItemCollection)workspace.GetItemCollection(DataSpace.OSpace); if (conceptualType is StructuralType) { var objectSpaceType = workspace.GetObjectSpaceType((StructuralType)conceptualType); return(itemCollection.GetClrType(objectSpaceType)); } else if (conceptualType is EnumType) { var objectSpaceType = workspace.GetObjectSpaceType((EnumType)conceptualType); return(itemCollection.GetClrType(objectSpaceType)); } else if (conceptualType is PrimitiveType) { return(((PrimitiveType)conceptualType).ClrEquivalentType); } else if (conceptualType is CollectionType) { return(workspace.GetClrTypeFromCSpaceType(((CollectionType)conceptualType).TypeUsage.EdmType)); } else if (conceptualType is RefType) { return(workspace.GetClrTypeFromCSpaceType(((RefType)conceptualType).ElementType)); } else if (conceptualType is EdmFunction) { return(workspace.GetClrTypeFromCSpaceType(((EdmFunction)conceptualType).ReturnParameter.TypeUsage.EdmType)); } return(null); }
public PropertyInfo Map(NavigationProperty navigationProperty) { var objectSpaceType = workspace.GetObjectSpaceType(navigationProperty.DeclaringType); Type type = typeLookup.Map(objectSpaceType); return(Map(type, navigationProperty.Name)); }
internal static Type GetClrType(MetadataWorkspace ocWorkspace, EnumType edmType) { var oSpaceType = (EnumType)ocWorkspace.GetObjectSpaceType(edmType); var objectItemCollection = (ObjectItemCollection)(ocWorkspace.GetItemCollection(DataSpace.OSpace)); return(objectItemCollection.GetClrType(oSpaceType)); }
private Type GetNavigationTargetType(NavigationProperty navigationProperty) { MetadataWorkspace metadataWorkspace = this._internalContext.ObjectContext.MetadataWorkspace; System.Data.Entity.Core.Metadata.Edm.EntityType entityType = navigationProperty.RelationshipType.RelationshipEndMembers.Single <RelationshipEndMember>((Func <RelationshipEndMember, bool>)(e => navigationProperty.ToEndMember.Name == e.Name)).GetEntityType(); StructuralType objectSpaceType = metadataWorkspace.GetObjectSpaceType((StructuralType)entityType); return(((ObjectItemCollection)metadataWorkspace.GetItemCollection(DataSpace.OSpace)).GetClrType(objectSpaceType)); }
private static IEdmEntityType CreateEntityType( MetadataWorkspace efModel, EntityType efEntityType, EdmModel model, IDictionary <MetadataItem, IEdmElement> elementMap, out List <EdmStructuralProperty> concurrencyProperties) { // TODO GitHubIssue#36 : support complex and entity inheritance var objectSpaceType = efModel.GetObjectSpaceType(efEntityType) as EntityType; var entityType = new EdmEntityType(objectSpaceType.NamespaceName, objectSpaceType.Name); concurrencyProperties = null; foreach (var efProperty in efEntityType.Properties) { var type = GetTypeReference(efProperty, model, elementMap); if (type != null) { string defaultValue = null; if (efProperty.DefaultValue != null) { defaultValue = efProperty.DefaultValue.ToString(); } var property = entityType.AddStructuralProperty( efProperty.Name, type, defaultValue, EdmConcurrencyMode.None); // alway None:replaced by OptimisticConcurrency annotation MetadataProperty storeGeneratedPattern = null; efProperty.MetadataProperties.TryGetValue( AnnotationSchema + StoreGeneratedPatternKey, true, out storeGeneratedPattern); if (storeGeneratedPattern != null && (string)storeGeneratedPattern.Value == StoreGeneratedPatternValueComputed) { SetComputedAnnotation(model, property); } if (efProperty.ConcurrencyMode == ConcurrencyMode.Fixed) { concurrencyProperties = concurrencyProperties ?? new List <EdmStructuralProperty>(); concurrencyProperties.Add(property); } } } entityType.AddKeys(efEntityType.KeyProperties .Select(p => entityType.FindProperty(p.Name)) .Cast <IEdmStructuralProperty>()); return(entityType); }
private static Type GetClrTypeFromCSpaceType(this MetadataWorkspace workspace, EdmProperty edmProperty) { EdmType edmType = null; if (edmProperty.IsEnumType) { edmType = edmProperty.EnumType; } else if (edmProperty.IsPrimitiveType) { edmType = edmProperty.PrimitiveType; } else if (edmProperty.IsUnderlyingPrimitiveType) { edmType = edmProperty.UnderlyingPrimitiveType; } else if (edmProperty.IsComplexType) { edmType = edmProperty.ComplexType; } var itemCollection = (ObjectItemCollection)workspace.GetItemCollection(DataSpace.OSpace); if (edmType is StructuralType) { var objectSpaceType = workspace.GetObjectSpaceType((StructuralType)edmType); return(itemCollection.GetClrType(objectSpaceType)); } else if (edmType is EnumType) { var objectSpaceType = workspace.GetObjectSpaceType((EnumType)edmType); return(itemCollection.GetClrType(objectSpaceType)); } else if (edmType is PrimitiveType) { return(((PrimitiveType)edmType).ClrEquivalentType); } return(null); }
/// <summary> /// Get from an EntityFramework conceptual model NavigationProperty /// to an actual reflection PropertyInfo. /// </summary> private PropertyInfo getPropertyInfo(NavigationProperty property) { var objectSpaceType = workspace.GetObjectSpaceType(property.DeclaringType); Type type; if (!typeLookup.TryGetValue(objectSpaceType.FullName, out type)) { // For some reason the type wasn't in the typeLookup we built when we // constructed this class. If we encountered any errors building that lookup // they will be in the typeLoadErrors collection we pass to the exception. throw new UnknownTypeException(objectSpaceType.FullName, typeLoadErrors); } return(getPropertyInfo(type, property.Name)); }
// <summary> // Updates the cache of types to entity sets either for the first time or after potentially // doing some o-space loading. // </summary> private void UpdateEntitySetMappings() { var objectItemCollection = (ObjectItemCollection)_workspace.GetItemCollection(DataSpace.OSpace); var ospaceTypes = _workspace.GetItems <EntityType>(DataSpace.OSpace); var inverseHierarchy = new Stack <EntityType>(); foreach (var ospaceType in ospaceTypes) { inverseHierarchy.Clear(); var cspaceType = (EntityType)_workspace.GetEdmSpaceType(ospaceType); do { inverseHierarchy.Push(cspaceType); cspaceType = (EntityType)cspaceType.BaseType; }while (cspaceType != null); EntitySet entitySet = null; while (entitySet == null && inverseHierarchy.Count > 0) { cspaceType = inverseHierarchy.Pop(); foreach (var container in _workspace.GetItems <EntityContainer>(DataSpace.CSpace)) { var entitySets = container.BaseEntitySets.Where(s => s.ElementType == cspaceType).ToList(); var entitySetsCount = entitySets.Count; if (entitySetsCount > 1 || entitySetsCount == 1 && entitySet != null) { throw Error.DbContext_MESTNotSupported(); } if (entitySetsCount == 1) { entitySet = (EntitySet)entitySets[0]; } } } // Entity set may be null if the o-space type is a base type that is in the model but is // not part of any set. For most practical purposes, this type is not in the model since // there is no way to query etc. for objects of this type. if (entitySet != null) { var ospaceBaseType = (EntityType)_workspace.GetObjectSpaceType(cspaceType); var clrType = objectItemCollection.GetClrType(ospaceType); var clrBaseType = objectItemCollection.GetClrType(ospaceBaseType); _entitySetMappingsCache[clrType] = new EntitySetTypePair(entitySet, clrBaseType); } } }
private static XDocument UpdateCSpaceOSpaceMappingCore(XDocument xDoc, MetadataWorkspace metadataWs) { var cspaceTypes = metadataWs.GetItems <System.Data.Entity.Core.Metadata.Edm.StructuralType>(DataSpace.CSpace); var tpls = cspaceTypes .Where(st => !(st is AssociationType)) .Select(st => { var ost = metadataWs.GetObjectSpaceType(st); return(new[] { st.FullName, ost.FullName }); }) .ToList(); var ocMapping = JsonConvert.SerializeObject(tpls); xDoc.Root.SetAttributeValue("CSpaceOSpaceMapping", ocMapping); return(xDoc); }
private static IEdmEntityType CreateEntityType( MetadataWorkspace efModel, EntityType efEntityType, EdmModel model, IDictionary<MetadataItem, IEdmElement> elementMap, out List<EdmStructuralProperty> concurrencyProperties) { // TODO GitHubIssue#36 : support complex and entity inheritance var objectSpaceType = efModel.GetObjectSpaceType(efEntityType) as EntityType; var entityType = new EdmEntityType(objectSpaceType.NamespaceName, objectSpaceType.Name); concurrencyProperties = null; foreach (var efProperty in efEntityType.Properties) { var type = GetTypeReference(efProperty, model, elementMap); if (type != null) { string defaultValue = null; if (efProperty.DefaultValue != null) { defaultValue = efProperty.DefaultValue.ToString(); } var property = entityType.AddStructuralProperty( efProperty.Name, type, defaultValue, EdmConcurrencyMode.None); // alway None:replaced by OptimisticConcurrency annotation MetadataProperty storeGeneratedPattern = null; efProperty.MetadataProperties.TryGetValue( AnnotationSchema + StoreGeneratedPatternKey, true, out storeGeneratedPattern); if (storeGeneratedPattern != null && (string)storeGeneratedPattern.Value == StoreGeneratedPatternValueComputed) { SetComputedAnnotation(model, property); } if (efProperty.ConcurrencyMode == ConcurrencyMode.Fixed) { concurrencyProperties = concurrencyProperties ?? new List<EdmStructuralProperty>(); concurrencyProperties.Add(property); } } } entityType.AddKeys(efEntityType.KeyProperties .Select(p => entityType.FindProperty(p.Name)) .Cast<IEdmStructuralProperty>()); return entityType; }
public OeEf6EdmModelMetadataProvider(DbContext dbContext) { MetadataWorkspace workspace = ((IObjectContextAdapter)dbContext).ObjectContext.MetadataWorkspace; var itemCollection = (ObjectItemCollection)workspace.GetItemCollection(DataSpace.OSpace); _entityTypes = workspace.GetItems <EntityType>(DataSpace.CSpace).ToDictionary(e => itemCollection.GetClrType(workspace.GetObjectSpaceType(e))); }