/// <summary> /// Initializes a new command tree with a given metadata workspace. /// </summary> /// <param name="metadata">The metadata workspace against which the command tree should operate.</param> /// <param name="dataSpace">The logical 'space' that metadata in the expressions used in this command tree must belong to.</param> internal DbCommandTree(MetadataWorkspace metadata, DataSpace dataSpace) { // Ensure the metadata workspace is non-null EntityUtil.CheckArgumentNull(metadata, "metadata"); // Ensure that the data space value is valid if (!DbCommandTree.IsValidDataSpace(dataSpace)) { throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_CommandTree_InvalidDataSpace, "dataSpace"); } // // Create the tree's metadata workspace and initalize commonly used types. // MetadataWorkspace effectiveMetadata = new MetadataWorkspace(); //While EdmItemCollection and StorageitemCollections are required //ObjectItemCollection may or may not be registered on the workspace yet. //So register the ObjectItemCollection if it exists. ItemCollection objectItemCollection; if (metadata.TryGetItemCollection(DataSpace.OSpace, out objectItemCollection)) { effectiveMetadata.RegisterItemCollection(objectItemCollection); } effectiveMetadata.RegisterItemCollection(metadata.GetItemCollection(DataSpace.CSpace)); effectiveMetadata.RegisterItemCollection(metadata.GetItemCollection(DataSpace.CSSpace)); effectiveMetadata.RegisterItemCollection(metadata.GetItemCollection(DataSpace.SSpace)); this._metadata = effectiveMetadata; this._dataSpace = dataSpace; }
/// <summary> /// Initializes a new command tree with a given metadata workspace. /// </summary> /// <param name="metadata">The metadata workspace against which the command tree should operate.</param> /// <param name="dataSpace">The logical 'space' that metadata in the expressions used in this command tree must belong to.</param> internal DbCommandTree(MetadataWorkspace metadata, DataSpace dataSpace) { // Ensure the metadata workspace is non-null //Contract.Requires(metadata != null); // Ensure that the data space value is valid if (!IsValidDataSpace(dataSpace)) { throw new ArgumentException(Strings.Cqt_CommandTree_InvalidDataSpace, "dataSpace"); } // // Create the tree's metadata workspace and initalize commonly used types. // var effectiveMetadata = new MetadataWorkspace(); //While EdmItemCollection and StorageitemCollections are required //ObjectItemCollection may or may not be registered on the workspace yet. //So register the ObjectItemCollection if it exists. ItemCollection objectItemCollection; if (metadata.TryGetItemCollection(DataSpace.OSpace, out objectItemCollection)) { effectiveMetadata.RegisterItemCollection(objectItemCollection); } effectiveMetadata.RegisterItemCollection(metadata.GetItemCollection(DataSpace.CSpace)); effectiveMetadata.RegisterItemCollection(metadata.GetItemCollection(DataSpace.CSSpace)); effectiveMetadata.RegisterItemCollection(metadata.GetItemCollection(DataSpace.SSpace)); _metadata = effectiveMetadata; _dataSpace = dataSpace; }
/// <summary> /// Retrieves a mapping to CLR type for the given EDM type. Assumes the MetadataWorkspace has no /// </summary> internal static ObjectTypeMapping GetObjectMapping(EdmType type, MetadataWorkspace workspace) { // Check if the workspace has cspace item collection registered with it. If not, then its a case // of public materializer trying to create objects from PODR or EntityDataReader with no context. ItemCollection collection; if (workspace.TryGetItemCollection(DataSpace.CSpace, out collection)) { return (ObjectTypeMapping)workspace.GetMap(type, DataSpace.OCSpace); } else { EdmType ospaceType; EdmType cspaceType; // If its a case of EntityDataReader with no context, the typeUsage which is passed in must contain // a cspace type. We need to look up an OSpace type in the ospace item collection and then create // ocMapping if (type.DataSpace == DataSpace.CSpace) { // if its a primitive type, then the names will be different for CSpace type and OSpace type if (Helper.IsPrimitiveType(type)) { ospaceType = workspace.GetMappedPrimitiveType(((PrimitiveType)type).PrimitiveTypeKind, DataSpace.OSpace); } else { // Metadata will throw if there is no item with this identity present. // Is this exception fine or does object materializer code wants to wrap and throw a new exception ospaceType = workspace.GetItem<EdmType>(type.FullName, DataSpace.OSpace); } cspaceType = type; } else { // In case of PODR, there is no cspace at all. We must create a fake ocmapping, with ospace types // on both the ends ospaceType = type; cspaceType = type; } // This condition must be hit only when someone is trying to materialize a legacy data reader and we // don't have the CSpace metadata. if (!Helper.IsPrimitiveType(ospaceType) && !Helper.IsEntityType(ospaceType) && !Helper.IsComplexType(ospaceType)) { throw EntityUtil.MaterializerUnsupportedType(); } ObjectTypeMapping typeMapping; if (Helper.IsPrimitiveType(ospaceType)) { typeMapping = new ObjectTypeMapping(ospaceType, cspaceType); } else { typeMapping = DefaultObjectMappingItemCollection.LoadObjectMapping(cspaceType, ospaceType, null); } return typeMapping; } }
/// <summary> /// Initializes a new command tree with a given metadata workspace. /// </summary> /// <param name="metadata">The metadata workspace against which the command tree should operate.</param> /// <param name="dataSpace">The logical 'space' that metadata in the expressions used in this command tree must belong to.</param> internal DbCommandTree(MetadataWorkspace metadata, DataSpace dataSpace) { // Ensure the metadata workspace is non-null EntityUtil.CheckArgumentNull(metadata, "metadata"); // Ensure that the data space value is valid if (!DbCommandTree.IsValidDataSpace(dataSpace)) { throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_CommandTree_InvalidDataSpace, "dataSpace"); } // // Create the tree's metadata workspace and initalize commonly used types. // MetadataWorkspace effectiveMetadata = new MetadataWorkspace(); //While EdmItemCollection and StorageitemCollections are required //ObjectItemCollection may or may not be registered on the workspace yet. //So register the ObjectItemCollection if it exists. ItemCollection objectItemCollection; if (metadata.TryGetItemCollection(DataSpace.OSpace, out objectItemCollection)) { effectiveMetadata.RegisterItemCollection(objectItemCollection); } effectiveMetadata.RegisterItemCollection(metadata.GetItemCollection(DataSpace.CSpace)); effectiveMetadata.RegisterItemCollection(metadata.GetItemCollection(DataSpace.CSSpace)); effectiveMetadata.RegisterItemCollection(metadata.GetItemCollection(DataSpace.SSpace)); this._metadata = effectiveMetadata; this._dataSpace = dataSpace; }
internal static ObjectTypeMapping GetObjectMapping( EdmType type, MetadataWorkspace workspace) { ItemCollection collection; if (workspace.TryGetItemCollection(DataSpace.CSpace, out collection)) { return((ObjectTypeMapping)workspace.GetMap((GlobalItem)type, DataSpace.OCSpace)); } EdmType edmType; EdmType cdmType; if (type.DataSpace == DataSpace.CSpace) { edmType = !Helper.IsPrimitiveType(type) ? workspace.GetItem <EdmType>(type.FullName, DataSpace.OSpace) : (EdmType)workspace.GetMappedPrimitiveType(((PrimitiveType)type).PrimitiveTypeKind, DataSpace.OSpace); cdmType = type; } else { edmType = type; cdmType = type; } if (!Helper.IsPrimitiveType(edmType) && !Helper.IsEntityType(edmType) && !Helper.IsComplexType(edmType)) { throw new NotSupportedException(Strings.Materializer_UnsupportedType); } return(!Helper.IsPrimitiveType(edmType) ? DefaultObjectMappingItemCollection.LoadObjectMapping(cdmType, edmType, (DefaultObjectMappingItemCollection)null) : new ObjectTypeMapping(edmType, cdmType)); }
internal static IEnumerable <EntitySet> GetInfluencingEntitySetsForTable( EntitySet table, MetadataWorkspace workspace) { ItemCollection collection = (ItemCollection)null; workspace.TryGetItemCollection(DataSpace.CSSpace, out collection); return(MappingMetadataHelper.GetEntityContainerMap((StorageMappingItemCollection)collection, table.EntityContainer).EntitySetMaps.Where <EntitySetBaseMapping>((Func <EntitySetBaseMapping, bool>)(map => map.TypeMappings.Any <TypeMapping>((Func <TypeMapping, bool>)(typeMap => typeMap.MappingFragments.Any <MappingFragment>((Func <MappingFragment, bool>)(mappingFrag => mappingFrag.TableSet.EdmEquals((MetadataItem)table))))))).Select <EntitySetBaseMapping, EntitySetBase>((Func <EntitySetBaseMapping, EntitySetBase>)(m => m.Set)).Cast <EntitySet>().Distinct <EntitySet>()); }
public void Registering_o_space_does_not_cause_oc_mapping_to_be_registered_if_c_space_is_not_registered() { var objectItemCollection = new ObjectItemCollection(); var workspace = new MetadataWorkspace(); #pragma warning disable 612,618 workspace.RegisterItemCollection(objectItemCollection); #pragma warning restore 612,618 Assert.Same(objectItemCollection, workspace.GetItemCollection(DataSpace.OSpace)); ItemCollection _; Assert.False(workspace.TryGetItemCollection(DataSpace.OCSpace, out _)); }
// <summary> // Returns overloads of a function with the given name in the target space. // </summary> // <param name="namespaceName"> namespace of the function </param> // <param name="functionName"> name of the function </param> // <param name="ignoreCase"> true for case-insensitive lookup </param> // <param name="functionOverloads"> function overloads </param> // <returns> returns true if a match was found, otherwise false </returns> internal bool TryGetFunctionByName( string namespaceName, string functionName, bool ignoreCase, out IList <EdmFunction> functionOverloads) { Check.NotEmpty(namespaceName, "namespaceName"); Check.NotEmpty(functionName, "functionName"); var fullName = namespaceName + "." + functionName; // First look for a model-defined function in the target space. var itemCollection = _metadataWorkspace.GetItemCollection(_targetDataspace); IList <EdmFunction> overloads = _targetDataspace == DataSpace.SSpace ? ((StoreItemCollection)itemCollection).GetCTypeFunctions(fullName, ignoreCase) : itemCollection.GetFunctions(fullName, ignoreCase); if (_targetDataspace == DataSpace.CSpace) { // Then look for a function import. if (overloads == null || overloads.Count == 0) { EntityContainer entityContainer; if (TryGetEntityContainer(namespaceName, /*ignoreCase:*/ false, out entityContainer)) { EdmFunction functionImport; if (TryGetFunctionImport(entityContainer, functionName, /*ignoreCase:*/ false, out functionImport)) { overloads = new[] { functionImport }; } } } // Last, look in SSpace. if (overloads == null || overloads.Count == 0) { ItemCollection storeItemCollection; if (_metadataWorkspace.TryGetItemCollection(DataSpace.SSpace, out storeItemCollection)) { overloads = ((StoreItemCollection)storeItemCollection).GetCTypeFunctions(fullName, ignoreCase); } } } functionOverloads = (overloads != null && overloads.Count > 0) ? overloads : null; return(functionOverloads != null); }
/// <summary> /// Tries to collect the views from the referenced assemblies of Entry assembly. /// </summary> /// <param name="workspace"></param> private void SerializedCollectViewsFromReferencedAssemblies(MetadataWorkspace workspace, Dictionary <EntitySetBase, GeneratedView> extentMappingViews) { ObjectItemCollection objectCollection; ItemCollection itemCollection; if (!workspace.TryGetItemCollection(DataSpace.OSpace, out itemCollection)) { //Possible enhancement : Think about achieving the same thing without creating Object Item Collection. objectCollection = new ObjectItemCollection(); itemCollection = objectCollection; // The GetEntryAssembly method can return a null reference //when a managed assembly has been loaded from an unmanaged application. Assembly entryAssembly = Assembly.GetEntryAssembly(); if (entryAssembly != null) { objectCollection.ImplicitLoadViewsFromAllReferencedAssemblies(entryAssembly); } } this.SerializedCollectViewsFromObjectCollection(workspace, extentMappingViews); }
/// <summary> /// Given a table EntitySet this function finds out all C-side EntitySets that are mapped to the table. /// </summary> internal static IEnumerable <EntitySet> GetInfluencingEntitySetsForTable(EntitySet table, MetadataWorkspace workspace) { Debug.Assert(table.EntityContainer.GetDataSpace() == DataSpace.SSpace); ItemCollection itemCollection = null; workspace.TryGetItemCollection(DataSpace.CSSpace, out itemCollection); StorageEntityContainerMapping containerMapping = MappingMetadataHelper.GetEntityContainerMap((StorageMappingItemCollection)itemCollection, table.EntityContainer); //find EntitySetMappings where one of the mapping fragment maps some type to the given table return(containerMapping.EntitySetMaps .Where( map => map.TypeMappings.Any( typeMap => typeMap.MappingFragments.Any( mappingFrag => mappingFrag.TableSet.EdmEquals(table) ) ) ) .Select(m => m.Set) .Cast <EntitySet>() .Distinct()); }
// <summary> // Retrieves a mapping to CLR type for the given EDM type. Assumes the MetadataWorkspace has no // </summary> internal static ObjectTypeMapping GetObjectMapping(EdmType type, MetadataWorkspace workspace) { // Check if the workspace has cspace item collection registered with it. If not, then its a case // of public materializer trying to create objects from PODR or EntityDataReader with no context. ItemCollection collection; if (workspace.TryGetItemCollection(DataSpace.CSpace, out collection)) { return((ObjectTypeMapping)workspace.GetMap(type, DataSpace.OCSpace)); } else { EdmType ospaceType; EdmType cspaceType; // If its a case of EntityDataReader with no context, the typeUsage which is passed in must contain // a cspace type. We need to look up an OSpace type in the ospace item collection and then create // ocMapping if (type.DataSpace == DataSpace.CSpace) { // if its a primitive type, then the names will be different for CSpace type and OSpace type if (Helper.IsPrimitiveType(type)) { ospaceType = workspace.GetMappedPrimitiveType(((PrimitiveType)type).PrimitiveTypeKind, DataSpace.OSpace); } else { // Metadata will throw if there is no item with this identity present. // Is this exception fine or does object materializer code wants to wrap and throw a new exception ospaceType = workspace.GetItem <EdmType>(type.FullName, DataSpace.OSpace); } cspaceType = type; } else { // In case of PODR, there is no cspace at all. We must create a fake ocmapping, with ospace types // on both the ends ospaceType = type; cspaceType = type; } // This condition must be hit only when someone is trying to materialize a legacy data reader and we // don't have the CSpace metadata. if (!Helper.IsPrimitiveType(ospaceType) && !Helper.IsEntityType(ospaceType) && !Helper.IsComplexType(ospaceType)) { throw new NotSupportedException(Strings.Materializer_UnsupportedType); } ObjectTypeMapping typeMapping; if (Helper.IsPrimitiveType(ospaceType)) { typeMapping = new ObjectTypeMapping(ospaceType, cspaceType); } else { typeMapping = DefaultObjectMappingItemCollection.LoadObjectMapping(cspaceType, ospaceType, null); } return(typeMapping); } }
public void Registering_o_space_does_not_cause_oc_mapping_to_be_registered_if_c_space_is_not_registered() { var objectItemCollection = new ObjectItemCollection(); var workspace = new MetadataWorkspace(); #pragma warning disable 612,618 workspace.RegisterItemCollection(objectItemCollection); #pragma warning restore 612,618 Assert.Same(objectItemCollection, workspace.GetItemCollection(DataSpace.OSpace)); ItemCollection _; Assert.False(workspace.TryGetItemCollection(DataSpace.OCSpace, out _)); }
/// <summary> /// Tries to collect the views from the referenced assemblies of Entry assembly. /// </summary> /// <param name="workspace"> </param> private void SerializedCollectViewsFromReferencedAssemblies( MetadataWorkspace workspace, Dictionary<EntitySetBase, GeneratedView> extentMappingViews) { ObjectItemCollection objectCollection; ItemCollection itemCollection; if (!workspace.TryGetItemCollection(DataSpace.OSpace, out itemCollection)) { //Possible enhancement : Think about achieving the same thing without creating Object Item Collection. objectCollection = new ObjectItemCollection(); itemCollection = objectCollection; // The GetEntryAssembly method can return a null reference //when a managed assembly has been loaded from an unmanaged application. var entryAssembly = Assembly.GetEntryAssembly(); if (entryAssembly != null) { objectCollection.ImplicitLoadViewsFromAllReferencedAssemblies(entryAssembly); } } SerializedCollectViewsFromObjectCollection(workspace, extentMappingViews); }
private bool SetEntityConnection(List<string> metadataPaths, EntityConnectionStringBuilder connStrBuilder) { // It's possible the metadata was specified in the original connection string, but we filtered out everything due to not being able to resolve it to anything. // In that case, warnings have already been displayed to indicate which paths were removed, so no need to display another message. if (metadataPaths.Count > 0) { try { // Get the connection first, because it might be needed to gather provider services information DbConnection dbConnection = GetDbConnection(connStrBuilder); MetadataWorkspace metadataWorkspace = new MetadataWorkspace(metadataPaths, _assemblies); // Ensure that we have all of the item collections registered. If some of them are missing this will cause problems eventually if we need to // execute a query to get detailed schema information, but that will be handled later. For now just register everything to prevent errors in the // stack that would not be understood by the user in the designer at this point. ItemCollection edmItemCollection; ItemCollection storeItemCollection; ItemCollection csItemCollection; if (!metadataWorkspace.TryGetItemCollection(DataSpace.CSpace, out edmItemCollection)) { edmItemCollection = new EdmItemCollection(); metadataWorkspace.RegisterItemCollection(edmItemCollection); } if (!metadataWorkspace.TryGetItemCollection(DataSpace.SSpace, out storeItemCollection)) { return false; } if (!metadataWorkspace.TryGetItemCollection(DataSpace.CSSpace, out csItemCollection)) { Debug.Assert(edmItemCollection != null && storeItemCollection != null, "edm and store ItemCollection should be populated already"); metadataWorkspace.RegisterItemCollection(new StorageMappingItemCollection(edmItemCollection as EdmItemCollection, storeItemCollection as StoreItemCollection)); } // Create an ObjectItemCollection beforehand so that we can load objects by-convention metadataWorkspace.RegisterItemCollection(new ObjectItemCollection()); // Load OSpace metadata from all of the assemblies we know about foreach (Assembly assembly in _assemblies) { metadataWorkspace.LoadFromAssembly(assembly); } if (dbConnection != null) { _entityConnection = new EntityConnection(metadataWorkspace, dbConnection); return true; } // else the DbConnection could not be created and the error should have already been displayed } catch (Exception ex) { StringBuilder exceptionMessage = new StringBuilder(); exceptionMessage.AppendLine(Strings.Error_MetadataLoadError); exceptionMessage.AppendLine(); exceptionMessage.Append(ex.Message); ShowError(exceptionMessage.ToString()); } } return false; }