public NavigationProperty Map(PropertyInfo property) { var objectSpaceType = workspace.GetItems <StructuralType>(DataSpace.OSpace).Single(i => i.Name == property.DeclaringType.Name); var cspaceType = workspace.GetEdmSpaceType(objectSpaceType); return((NavigationProperty)cspaceType.Members.Single(m => m.Name == property.Name)); }
private EntityType GetTableEntityType(Type DBType) { ObjectContext objContext = ((IObjectContextAdapter)DB).ObjectContext; MetadataWorkspace workspace = objContext.MetadataWorkspace; EntityType table = workspace.GetEdmSpaceType((StructuralType)workspace.GetItem <EntityType>(DBType.FullName, DataSpace.OSpace)) as EntityType; return(table); }
private static StructuralType GetEdmType(MetadataWorkspace workspace) { StructuralType objectSpaceType; workspace.LoadFromAssembly(typeof(T).Assembly); if (!workspace.TryGetItem <StructuralType>(typeof(T).FullName, DataSpace.OSpace, out objectSpaceType)) { //throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Messages.UnableToFindMetadataForType, typeof(T))); } StructuralType edmSpaceType = workspace.GetEdmSpaceType(objectSpaceType); return(edmSpaceType); }
// <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 EntityType GetEdmSpaceType(this DbContext dbContext, Type entityType) { MetadataWorkspace workspace = ((IObjectContextAdapter)dbContext) .ObjectContext.MetadataWorkspace; StructuralType oType = workspace.GetItems <StructuralType>(DataSpace.OSpace) .Where(e => e.FullName == entityType.FullName).SingleOrDefault(); if (oType == null) { return(null); } return(workspace.GetEdmSpaceType(oType) as EntityType); }
private static Dictionary <Type, EntitySet> CreateEntitySetMappings(MetadataWorkspace metadataWorkspace) { var entitySetMappings = new Dictionary <Type, EntitySet>(); var itemCollection = (ObjectItemCollection)metadataWorkspace.GetItemCollection(DataSpace.OSpace); var entityTypes = metadataWorkspace.GetItems <EntityType>(DataSpace.OSpace); var entityContainers = metadataWorkspace.GetItems <EntityContainer>(DataSpace.CSpace); var stack = new Stack <EntityType>(); if (entityTypes == null || entityContainers == null) { return(entitySetMappings); } foreach (EntityType type in entityTypes) { Func <EntitySetBase, bool> predicate = null; stack.Clear(); var cspaceType = (EntityType)metadataWorkspace.GetEdmSpaceType(type); do { stack.Push(cspaceType); cspaceType = (EntityType)cspaceType.BaseType; } while (cspaceType != null); EntitySet entitySet = null; while ((entitySet == null) && (stack.Count > 0)) { cspaceType = stack.Pop(); foreach (EntityContainer container in entityContainers) { if (predicate == null) { predicate = s => s.ElementType == cspaceType; } var source = container.BaseEntitySets .Where(predicate) .ToList(); int count = source.Count(); if ((count > 1) || ((count == 1) && (entitySet != null))) { throw new InvalidOperationException("Multiple entity sets per type is not supported."); } if (count == 1) { entitySet = (EntitySet)source.First(); } } } if (entitySet == null) { continue; } Type clrType = itemCollection.GetClrType(type); entitySetMappings[clrType] = entitySet; } return(entitySetMappings); }