/// <summary>
            /// Collect the views from object collection's view gen assembly
            /// </summary>
            /// <param name="workspace"></param>
            /// <param name="objectCollection"></param>
            private void SerializedCollectViewsFromObjectCollection(MetadataWorkspace workspace, Dictionary <EntitySetBase, GeneratedView> extentMappingViews)
            {
                IList <Assembly> allViewGenAssemblies = ObjectItemCollection.ViewGenerationAssemblies;

                if (allViewGenAssemblies != null)
                {
                    foreach (Assembly assembly in allViewGenAssemblies)
                    {
                        object[] viewGenAttributes = assembly.GetCustomAttributes(typeof(System.Data.Mapping.EntityViewGenerationAttribute), false /*inherit*/);
                        if ((viewGenAttributes != null) && (viewGenAttributes.Length != 0))
                        {
                            foreach (EntityViewGenerationAttribute viewGenAttribute in viewGenAttributes)
                            {
                                Type viewContainerType = viewGenAttribute.ViewGenerationType;
                                if (!viewContainerType.IsSubclassOf(typeof(EntityViewContainer)))
                                {
                                    throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.Generated_View_Type_Super_Class(StorageMslConstructs.EntityViewGenerationTypeName));
                                }
                                EntityViewContainer viewContainer = (Activator.CreateInstance(viewContainerType) as EntityViewContainer);
                                Debug.Assert(viewContainer != null, "Should be able to create the type");

                                SerializedAddGeneratedViewsInEntityViewContainer(workspace, viewContainer, extentMappingViews);
                            }
                        }
                    }
                }
            }
            //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 (KeyValuePair <string, string> extentView in viewContainer.ExtentViews)
                {
                    EntityContainer entityContainer = null;
                    EntitySetBase   extent          = null;


                    string extentFullName  = extentView.Key;
                    int    extentNameIndex = extentFullName.LastIndexOf('.');

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

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

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

                    if (extent == null)
                    {
                        throw new MappingException(System.Data.Entity.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 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);
            }
            private 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");

                string viewHash       = MetadataHelper.GenerateHashForAllExtentViewsContent(mappingCollection.MappingVersion, viewContainer.ExtentViews);
                string storedViewHash = viewContainer.HashOverAllExtentViews;

                if (viewHash != storedViewHash)
                {
                    return(false);
                }
                return(true);
            }
            //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 (KeyValuePair<string, string> extentView in viewContainer.ExtentViews)
                {
                    EntityContainer entityContainer = null;
                    EntitySetBase extent = null;


                    string extentFullName = extentView.Key;
                    int extentNameIndex = extentFullName.LastIndexOf('.');

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

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

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

                    if (extent == null)
                    {
                        throw new MappingException(System.Data.Entity.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 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");

                string viewHash = MetadataHelper.GenerateHashForAllExtentViewsContent(mappingCollection.MappingVersion, viewContainer.ExtentViews);
                string 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 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 (!this.TryGetCorrespondingStorageEntityContainerMapping(entityViewContainer,
                    workspace.GetItemCollection(DataSpace.CSSpace).GetItems<StorageEntityContainerMapping>(), out storageEntityContainerMapping))
                {
                    return;
                }

                // second check
                if (!this.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 (this.VerifyViewsHaveNotChanged(workspace, entityViewContainer))
                {
                    this.SerializedAddGeneratedViews(workspace, entityViewContainer, extentMappingViews);
                }
                else
                {
                    throw new InvalidOperationException(System.Data.Entity.Strings.Generated_Views_Changed);
                }
            }
 private bool SerializedVerifyHashOverMmClosure(StorageEntityContainerMapping entityContainerMapping, EntityViewContainer entityViewContainer)
 {
     if (MetadataMappingHasherVisitor.GetMappingClosureHash(m_storageMappingItemCollection.MappingVersion, entityContainerMapping) ==
         entityViewContainer.HashOverMappingClosure)
     {
         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 (!this.TryGetCorrespondingStorageEntityContainerMapping(entityViewContainer,
                                                                           workspace.GetItemCollection(DataSpace.CSSpace).GetItems <StorageEntityContainerMapping>(), out storageEntityContainerMapping))
                {
                    return;
                }

                // second check
                if (!this.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 (this.VerifyViewsHaveNotChanged(workspace, entityViewContainer))
                {
                    this.SerializedAddGeneratedViews(workspace, entityViewContainer, extentMappingViews);
                }
                else
                {
                    throw new InvalidOperationException(System.Data.Entity.Strings.Generated_Views_Changed);
                }
            }