//Collect the names of the entitysetbases and the generated views from
            //the generated type into a string so that we can produce a hash over it.
            private void SerializedAddGeneratedViews(
                MetadataWorkspace workspace, EntityViewContainer viewContainer, Dictionary<EntitySetBase, GeneratedView> extentMappingViews)
            {
                foreach (var extentView in viewContainer.ExtentViews)
                {
                    EntityContainer entityContainer = null;
                    EntitySetBase extent = null;

                    var extentFullName = extentView.Key;
                    var extentNameIndex = extentFullName.LastIndexOf('.');

                    if (extentNameIndex != -1)
                    {
                        var entityContainerName = extentFullName.Substring(0, extentNameIndex);
                        var extentName = extentFullName.Substring(extentFullName.LastIndexOf('.') + 1);

                        if (!workspace.TryGetItem(entityContainerName, DataSpace.CSpace, out entityContainer))
                        {
                            workspace.TryGetItem(entityContainerName, DataSpace.SSpace, out entityContainer);
                        }

                        if (entityContainer != null)
                        {
                            entityContainer.BaseEntitySets.TryGetValue(extentName, false, out extent);
                        }
                    }

                    if (extent == null)
                    {
                        throw new MappingException(System.Data.Entity.Resources.Strings.Generated_Views_Invalid_Extent(extentFullName));
                    }

                    //Create a Generated view and cache it
                    GeneratedView generatedView;
                    //Add the view to the local dictionary
                    if (!extentMappingViews.TryGetValue(extent, out generatedView))
                    {
                        generatedView = GeneratedView.CreateGeneratedView(
                            extent,
                            null, // edmType
                            null, // commandTree
                            extentView.Value, // eSQL
                            m_storageMappingItemCollection,
                            new ConfigViewGenerator());
                        extentMappingViews.Add(extent, generatedView);
                    }
                }
            }
            private static bool VerifyViewsHaveNotChanged(MetadataWorkspace workspace, EntityViewContainer viewContainer)
            {
                //Now check whether the hash of the generated views match the one
                //we stored in the code file during design
                //Produce the hash and add it to the code
                var mappingCollection = (workspace.GetItemCollection(DataSpace.CSSpace) as StorageMappingItemCollection);
                Debug.Assert(mappingCollection != null, "Must have Mapping Collection in the Metadataworkspace");

                var viewHash = MetadataHelper.GenerateHashForAllExtentViewsContent(
                    mappingCollection.MappingVersion, viewContainer.ExtentViews);
                var storedViewHash = viewContainer.HashOverAllExtentViews;
                if (viewHash != storedViewHash)
                {
                    return false;
                }
                return true;
            }
 private bool SerializedVerifyHashOverMmClosure(
     StorageEntityContainerMapping entityContainerMapping, EntityViewContainer entityViewContainer)
 {
     if (MetadataMappingHasherVisitor.GetMappingClosureHash(
         m_storageMappingItemCollection.MappingVersion, entityContainerMapping)
         ==
         entityViewContainer.HashOverMappingClosure)
     {
         return true;
     }
     return false;
 }
            private static bool TryGetCorrespondingStorageEntityContainerMapping(
                EntityViewContainer viewContainer,
                IEnumerable<StorageEntityContainerMapping> storageEntityContainerMappingList,
                out StorageEntityContainerMapping storageEntityContainerMapping)
            {
                storageEntityContainerMapping = null;

                foreach (var entityContainerMapping in storageEntityContainerMappingList)
                {
                    // first check
                    if (entityContainerMapping.EdmEntityContainer.Name == viewContainer.EdmEntityContainerName
                        &&
                        entityContainerMapping.StorageEntityContainer.Name == viewContainer.StoreEntityContainerName)
                    {
                        storageEntityContainerMapping = entityContainerMapping;
                        return true;
                    }
                }
                return false;
            }
            /// <summary>
            ///     this method do the following check on the generated views in the EntityViewContainer, 
            ///     then add those views all at once to the dictionary
            ///     1. there should be one storeageEntityContainerMapping that has the same h
            ///     C side and S side names as the EnittyViewcontainer
            ///     2. Generate the hash for the storageEntityContainerMapping in the MM closure, 
            ///     and this hash should be the same in EntityViewContainer
            ///     3. Generate the hash for all of the view text in the EntityViewContainer and 
            ///     this hash should be the same as the stored on in the EntityViewContainer
            /// </summary>
            /// <param name="entityViewContainer"> </param>
            private void SerializedAddGeneratedViewsInEntityViewContainer(
                MetadataWorkspace workspace, EntityViewContainer entityViewContainer,
                Dictionary<EntitySetBase, GeneratedView> extentMappingViews)
            {
                StorageEntityContainerMapping storageEntityContainerMapping;
                // first check
                if (!TryGetCorrespondingStorageEntityContainerMapping(
                    entityViewContainer,
                    workspace.GetItemCollection(DataSpace.CSSpace).GetItems<StorageEntityContainerMapping>(),
                    out storageEntityContainerMapping))
                {
                    return;
                }

                // second check
                if (!SerializedVerifyHashOverMmClosure(storageEntityContainerMapping, entityViewContainer))
                {
                    throw new MappingException(
                        Strings.ViewGen_HashOnMappingClosure_Not_Matching(entityViewContainer.EdmEntityContainerName));
                }

                // third check, prior to the check, we collect all the views in the entity view container to the dictionary
                // if the views are changed then we will throw exception out
                if (VerifyViewsHaveNotChanged(workspace, entityViewContainer))
                {
                    SerializedAddGeneratedViews(workspace, entityViewContainer, extentMappingViews);
                }
                else
                {
                    throw new InvalidOperationException(System.Data.Entity.Resources.Strings.Generated_Views_Changed);
                }
            }
            /// <summary>
///     this method do the following check on the generated views in the EntityViewContainer,
///     then add those views all at once to the dictionary
///     1. there should be one storeageEntityContainerMapping that has the same h
///     C side and S side names as the EnittyViewcontainer
///     2. Generate the hash for the storageEntityContainerMapping in the MM closure,
///     and this hash should be the same in EntityViewContainer
///     3. Generate the hash for all of the view text in the EntityViewContainer and
///     this hash should be the same as the stored on in the EntityViewContainer
/// </summary>
/// <param name="entityViewContainer"> </param>
            private void SerializedAddGeneratedViewsInEntityViewContainer(
                MetadataWorkspace workspace, EntityViewContainer entityViewContainer,
                Dictionary<EntitySetBase, GeneratedView> extentMappingViews)
            {
                StorageEntityContainerMapping storageEntityContainerMapping;
                // first check
                if (!TryGetCorrespondingStorageEntityContainerMapping(
                    entityViewContainer,
                    workspace.GetItemCollection(DataSpace.CSSpace).GetItems<StorageEntityContainerMapping>(),
                    out storageEntityContainerMapping))
                {
                    return;
                }

                // second check
                if (!SerializedVerifyHashOverMmClosure(storageEntityContainerMapping, entityViewContainer))
                {
                    throw new MappingException(
                        Strings.ViewGen_HashOnMappingClosure_Not_Matching(entityViewContainer.EdmEntityContainerName));
                }

                SerializedAddGeneratedViews(workspace, entityViewContainer, extentMappingViews);
            }