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>());
        }
            private Dictionary <EntitySetBase, GeneratedView> SerializedGetGeneratedViews(EntityContainer container)
            {
                Debug.Assert(container != null);

                // Note that extentMappingViews will contain both query and update views.
                Dictionary <EntitySetBase, GeneratedView> extentMappingViews;

                // Get the mapping that has the entity container mapped.
                StorageEntityContainerMapping entityContainerMap = MappingMetadataHelper.GetEntityContainerMap(m_storageMappingItemCollection, container);

                // We get here because memoizer didn't find an entry for the container.
                // It might happen that the entry with generated views already exists for the counterpart container, so check it first.
                EntityContainer counterpartContainer = container.DataSpace == DataSpace.CSpace ?
                                                       entityContainerMap.StorageEntityContainer : entityContainerMap.EdmEntityContainer;

                if (m_generatedViewsMemoizer.TryGetValue(counterpartContainer, out extentMappingViews))
                {
                    return(extentMappingViews);
                }

                extentMappingViews = new Dictionary <EntitySetBase, GeneratedView>();

                if (!entityContainerMap.HasViews)
                {
                    return(extentMappingViews);
                }

                // If we are in generated views mode.
                if (m_generatedViewsMode)
                {
                    if (ObjectItemCollection.ViewGenerationAssemblies != null && ObjectItemCollection.ViewGenerationAssemblies.Count > 0)
                    {
                        SerializedCollectViewsFromObjectCollection(this.m_storageMappingItemCollection.Workspace, extentMappingViews);
                    }
                    else
                    {
                        SerializedCollectViewsFromReferencedAssemblies(this.m_storageMappingItemCollection.Workspace, extentMappingViews);
                    }
                }

                if (extentMappingViews.Count == 0)
                {
                    // We should change the mode to runtime generation of views.
                    this.m_generatedViewsMode = false;
                    this.SerializedGenerateViews(entityContainerMap, extentMappingViews);
                }

                Debug.Assert(extentMappingViews.Count > 0, "view should be generated at this point");

                return(extentMappingViews);
            }
Esempio n. 3
0
        /// <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>
        /// Finds interesting members for MetdataWorkspace.GetRequiredOriginalValueMembers() and MetdataWorkspace.GetRelevantMembersForUpdate() methods
        /// for the given <paramref name="entitySet"/> and <paramref name="entityType"/>.
        /// </summary>
        /// <param name="entitySet">An EntitySet belonging to the C-Space. Must not be null.</param>
        /// <param name="entityType">An EntityType that participates in the given EntitySet. Must not be null.</param>
        /// <param name="interestingMembersKind">Scenario the members should be returned for.</param>
        /// <returns>ReadOnlyCollection of interesting members for the requested scenario (<paramref name="interestingMembersKind"/>).</returns>
        private ReadOnlyCollection <EdmMember> FindInterestingMembers(EntitySetBase entitySet, EntityTypeBase entityType, InterestingMembersKind interestingMembersKind)
        {
            Debug.Assert(entitySet != null, "entitySet != null");
            Debug.Assert(entityType != null, "entityType != null");

            var interestingMembers = new List <EdmMember>();

            foreach (var storageTypeMapping in MappingMetadataHelper.GetMappingsForEntitySetAndSuperTypes(this, entitySet.EntityContainer, entitySet, entityType))
            {
                StorageAssociationTypeMapping associationTypeMapping = storageTypeMapping as StorageAssociationTypeMapping;
                if (associationTypeMapping != null)
                {
                    FindInterestingAssociationMappingMembers(associationTypeMapping, interestingMembers);
                }
                else
                {
                    Debug.Assert(storageTypeMapping is StorageEntityTypeMapping, "StorageEntityTypeMapping expected.");

                    FindInterestingEntityMappingMembers((StorageEntityTypeMapping)storageTypeMapping, interestingMembersKind, interestingMembers);
                }
            }

            // For backwards compatibility we don't return foreign keys from the obsolete MetadataWorkspace.GetRequiredOriginalValueMembers() method
            if (interestingMembersKind != InterestingMembersKind.RequiredOriginalValueMembers)
            {
                FindForeignKeyProperties(entitySet, entityType, interestingMembers);
            }

            foreach (var functionMappings in MappingMetadataHelper
                     .GetModificationFunctionMappingsForEntitySetAndType(this, entitySet.EntityContainer, entitySet, entityType)
                     .Where(functionMappings => functionMappings.UpdateFunctionMapping != null))
            {
                FindInterestingFunctionMappingMembers(functionMappings, interestingMembersKind, ref interestingMembers);
            }

            Debug.Assert(interestingMembers != null, "interestingMembers must never be null.");

            return(new ReadOnlyCollection <EdmMember>(interestingMembers.Distinct().ToList()));
        }
            /// <summary>
            /// Generates a single query view for a given Extent and type. It is used to generate OfType and OfTypeOnly views.
            /// </summary>
            /// <param name="includeSubtypes">Whether the view should include extents that are subtypes of the given entity</param>
            private bool TryGenerateQueryViewOfType(EntityContainer entityContainer, EntitySetBase entity, EntityTypeBase type, bool includeSubtypes, out GeneratedView generatedView)
            {
                Debug.Assert(entityContainer != null);
                Debug.Assert(entity != null);
                Debug.Assert(type != null);

                if (type.Abstract)
                {
                    generatedView = null;
                    return(false);
                }

                //Get the mapping that has the entity container mapped.
                StorageEntityContainerMapping entityContainerMap = MappingMetadataHelper.GetEntityContainerMap(m_storageMappingItemCollection, entityContainer);

                Debug.Assert(!entityContainerMap.IsEmpty, "There are no entity set maps");

                bool           success;
                ViewGenResults viewGenResults = ViewgenGatekeeper.GenerateTypeSpecificQueryView(entityContainerMap, m_config, entity, type, includeSubtypes, out success);

                if (!success)
                {
                    generatedView = null;
                    return(false); //could not generate view
                }

                KeyToListMap <EntitySetBase, GeneratedView> extentMappingViews = viewGenResults.Views;

                if (viewGenResults.HasErrors)
                {
                    throw new MappingException(Helper.CombineErrorMessage(viewGenResults.Errors));
                }

                Debug.Assert(extentMappingViews.AllValues.Count() == 1, "Viewgen should have produced only one view");
                generatedView = extentMappingViews.AllValues.First();

                return(true);
            }