// <summary>
        // Load stuff from xml readers - this now includes XmlReader instances created over embedded
        // resources. See the remarks section below for some useful information.
        // </summary>
        // <param name="xmlReaders"> A list of XmlReader instances </param>
        // <param name="dataModelOption"> whether this is a entity data model or provider data model </param>
        // <param name="providerManifest"> provider manifest from which the primitive type definition comes from </param>
        // <param name="itemCollection"> item collection to add the item after loading </param>
        internal static IList <EdmSchemaError> LoadItems(
            IEnumerable <XmlReader> xmlReaders,
            IEnumerable <string> sourceFilePaths,
            SchemaDataModelOption dataModelOption,
            DbProviderManifest providerManifest,
            ItemCollection itemCollection,
            bool throwOnError)
        {
            IList <Schema> schemaCollection = null;

            // Parse and validate all the schemas - since we support using now,
            // we need to parse them as a group
            var errorCollection = SchemaManager.ParseAndValidate(
                xmlReaders, sourceFilePaths,
                dataModelOption, providerManifest, out schemaCollection);

            // Try to initialize the metadata if there are no errors
            if (MetadataHelper.CheckIfAllErrorsAreWarnings(errorCollection))
            {
                var errors = LoadItems(providerManifest, schemaCollection, itemCollection);
                foreach (var error in errors)
                {
                    errorCollection.Add(error);
                }
            }
            if (!MetadataHelper.CheckIfAllErrorsAreWarnings(errorCollection) && throwOnError)
            {
                //Future Enhancement: if there is an error, we throw exception with error and warnings.
                //Otherwise the user has no clue to know about warnings.
                throw EntityUtil.InvalidSchemaEncountered(Helper.CombineErrorMessage(errorCollection));
            }
            return(errorCollection);
        }
Exemple #2
0
 internal void ThrowOnNonWarningErrors()
 {
     if (!MetadataHelper.CheckIfAllErrorsAreWarnings(this._errors))
     {
         throw EntityUtil.InvalidSchemaEncountered(Helper.CombineErrorMessage((IEnumerable <EdmSchemaError>) this._errors));
     }
 }
Exemple #3
0
 internal void ThrowOnNonWarningErrors()
 {
     if (!MetadataHelper.CheckIfAllErrorsAreWarnings(_errors))
     {
         //Future Enhancement: if there is an error, we throw exception with error and warnings.
         //Otherwise the user has no clue to know about warnings.
         throw EntityUtil.InvalidSchemaEncountered(Helper.CombineErrorMessage(_errors));
     }
 }
        public virtual Dictionary <string, EdmType> LoadTypesExpensiveWay(
            Assembly assembly)
        {
            KnownAssembliesSet           knownAssemblies = new KnownAssembliesSet();
            Dictionary <string, EdmType> typesInLoading;
            List <EdmItemError>          errors;

            AssemblyCache.LoadAssembly(assembly, false, knownAssemblies, out typesInLoading, out errors);
            if (errors.Count != 0)
            {
                throw EntityUtil.InvalidSchemaEncountered(Helper.CombineErrorMessage((IEnumerable <EdmItemError>)errors));
            }
            return(typesInLoading);
        }
Exemple #5
0
        /// <summary>
        ///     Loads the OSpace types in the assembly and returns them as a dictionary
        /// </summary>
        /// <param name="assembly"> The assembly to load </param>
        /// <returns> A mapping from names to OSpace EdmTypes </returns>
        internal static Dictionary <string, EdmType> LoadTypesExpensiveWay(Assembly assembly)
        {
            Dictionary <string, EdmType> typesInLoading = null;

            List <EdmItemError> errors;
            var knownAssemblies = new KnownAssembliesSet();

            AssemblyCache.LoadAssembly(
                assembly, false /*loadAllReferencedAssemblies*/,
                knownAssemblies, out typesInLoading, out errors);

            // Check for errors
            if (errors.Count != 0)
            {
                throw EntityUtil.InvalidSchemaEncountered(Helper.CombineErrorMessage(errors));
            }

            return(typesInLoading);
        }
Exemple #6
0
        internal static IList <EdmSchemaError> LoadItems(
            IEnumerable <XmlReader> xmlReaders,
            IEnumerable <string> sourceFilePaths,
            SchemaDataModelOption dataModelOption,
            DbProviderManifest providerManifest,
            ItemCollection itemCollection,
            bool throwOnError)
        {
            IList <Schema>         schemaCollection = (IList <Schema>)null;
            IList <EdmSchemaError> andValidate      = SchemaManager.ParseAndValidate(xmlReaders, sourceFilePaths, dataModelOption, providerManifest, out schemaCollection);

            if (MetadataHelper.CheckIfAllErrorsAreWarnings(andValidate))
            {
                foreach (EdmSchemaError loadItem in EdmItemCollection.LoadItems(providerManifest, schemaCollection, itemCollection))
                {
                    andValidate.Add(loadItem);
                }
            }
            if (!MetadataHelper.CheckIfAllErrorsAreWarnings(andValidate) && throwOnError)
            {
                throw EntityUtil.InvalidSchemaEncountered(Helper.CombineErrorMessage((IEnumerable <EdmSchemaError>)andValidate));
            }
            return(andValidate);
        }
        private bool LoadAssemblyFromCache(
            Assembly assembly, bool loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action <String> logLoadMessage)
        {
            // Code First already did type loading
            if (OSpaceTypesLoaded)
            {
                return(true);
            }

            // If all the containers (usually only one) have the UseClrTypes annotation then use the Code First loader even
            // when using an EDMX.
            if (edmItemCollection != null)
            {
                var containers = edmItemCollection.GetItems <EntityContainer>();
                if (containers.Any() &&
                    containers.All(
                        c => c.Annotations.Any(
                            a => a.Name == XmlConstants.UseClrTypesAnnotationWithPrefix &&
                            ((string)a.Value).ToUpperInvariant() == "TRUE")))
                {
                    lock (LoadAssemblyLock)
                    {
                        if (!OSpaceTypesLoaded)
                        {
                            new CodeFirstOSpaceLoader().LoadTypes(edmItemCollection, this);

                            Debug.Assert(OSpaceTypesLoaded);
                        }
                        return(true);
                    }
                }
            }

            // Check if its loaded in the cache - if the call is for loading referenced assemblies, make sure that all referenced
            // assemblies are also loaded
            KnownAssemblyEntry entry;

            if (_knownAssemblies.TryGetKnownAssembly(assembly, _loaderCookie, edmItemCollection, out entry))
            {
                // Proceed if only we need to load the referenced assemblies and they are not loaded
                if (loadReferencedAssemblies == false)
                {
                    // don't say we loaded anything, unless we actually did before
                    return(entry.CacheEntry.TypesInAssembly.Count != 0);
                }
                else if (entry.ReferencedAssembliesAreLoaded)
                {
                    // this assembly was part of a all hands reference search
                    return(true);
                }
            }

            lock (LoadAssemblyLock)
            {
                // Check after acquiring the lock, since the known assemblies might have got modified
                // Check if the assembly is already loaded. The reason we need to check if the assembly is already loaded, is that
                if (_knownAssemblies.TryGetKnownAssembly(assembly, _loaderCookie, edmItemCollection, out entry))
                {
                    // Proceed if only we need to load the referenced assemblies and they are not loaded
                    if (loadReferencedAssemblies == false ||
                        entry.ReferencedAssembliesAreLoaded)
                    {
                        return(true);
                    }
                }

                Dictionary <string, EdmType> typesInLoading;
                List <EdmItemError>          errors;
                var knownAssemblies = new KnownAssembliesSet(_knownAssemblies);

                // Load the assembly from the cache
                AssemblyCache.LoadAssembly(
                    assembly, loadReferencedAssemblies, knownAssemblies, edmItemCollection, logLoadMessage,
                    ref _loaderCookie, out typesInLoading, out errors);

                // Throw if we have encountered errors
                if (errors.Count != 0)
                {
                    throw EntityUtil.InvalidSchemaEncountered(Helper.CombineErrorMessage(errors));
                }

                // We can encounter new assemblies, but they may not have any time in them
                if (typesInLoading.Count != 0)
                {
                    // No errors, so go ahead and add the types and make them readonly
                    // The existence of the loading lock tells us whether we should be thread safe or not, if we need
                    // to be thread safe. We don't need to actually use the lock because the caller should have done
                    // it already.
                    // Recheck the assemblies added, another list is created just to match up the collection type
                    // taken in by AddRange()
                    AddLoadedTypes(typesInLoading);
                }

                // Update the value of known assemblies
                _knownAssemblies = knownAssemblies;

                return(typesInLoading.Count != 0);
            }
        }
Exemple #8
0
        private static bool LoadAssemblyFromCache(
            ObjectItemCollection objectItemCollection, Assembly assembly,
            bool loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action <String> logLoadMessage)
        {
            // Check if its loaded in the cache - if the call is for loading referenced assemblies, make sure that all referenced
            // assemblies are also loaded
            KnownAssemblyEntry entry;

            if (objectItemCollection._knownAssemblies.TryGetKnownAssembly(
                    assembly, objectItemCollection._loaderCookie, edmItemCollection, out entry))
            {
                // Proceed if only we need to load the referenced assemblies and they are not loaded
                if (loadReferencedAssemblies == false)
                {
                    // don't say we loaded anything, unless we actually did before
                    return(entry.CacheEntry.TypesInAssembly.Count != 0);
                }
                else if (entry.ReferencedAssembliesAreLoaded)
                {
                    // this assembly was part of a all hands reference search
                    return(true);
                }
            }

            lock (objectItemCollection.LoadAssemblyLock)
            {
                // Check after acquiring the lock, since the known assemblies might have got modified
                // Check if the assembly is already loaded. The reason we need to check if the assembly is already loaded, is that
                if (objectItemCollection._knownAssemblies.TryGetKnownAssembly(
                        assembly, objectItemCollection._loaderCookie, edmItemCollection, out entry))
                {
                    // Proceed if only we need to load the referenced assemblies and they are not loaded
                    if (loadReferencedAssemblies == false ||
                        entry.ReferencedAssembliesAreLoaded)
                    {
                        return(true);
                    }
                }

                Dictionary <string, EdmType> typesInLoading;
                List <EdmItemError>          errors;
                KnownAssembliesSet           knownAssemblies;

                if (objectItemCollection != null)
                {
                    knownAssemblies = new KnownAssembliesSet(objectItemCollection._knownAssemblies);
                }
                else
                {
                    knownAssemblies = new KnownAssembliesSet();
                }

                // Load the assembly from the cache
                AssemblyCache.LoadAssembly(
                    assembly, loadReferencedAssemblies, knownAssemblies, edmItemCollection, logLoadMessage,
                    ref objectItemCollection._loaderCookie, out typesInLoading, out errors);

                // Throw if we have encountered errors
                if (errors.Count != 0)
                {
                    throw EntityUtil.InvalidSchemaEncountered(Helper.CombineErrorMessage(errors));
                }

                // We can encounter new assemblies, but they may not have any time in them
                if (typesInLoading.Count != 0)
                {
                    // No errors, so go ahead and add the types and make them readonly
                    // The existence of the loading lock tells us whether we should be thread safe or not, if we need
                    // to be thread safe, then we need to use AtomicAddRange. We don't need to actually use the lock
                    // because the caller should have done it already
                    // Recheck the assemblies added, another list is created just to match up the collection type
                    // taken in by AtomicAddRange()
                    var globalItems = new List <GlobalItem>();
                    foreach (var edmType in typesInLoading.Values)
                    {
                        globalItems.Add(edmType);

                        var cspaceTypeName = "";
                        try
                        {
                            // Also populate the ocmapping information
                            if (Helper.IsEntityType(edmType))
                            {
                                cspaceTypeName = ((ClrEntityType)edmType).CSpaceTypeName;
                                objectItemCollection._ocMapping.Add(cspaceTypeName, edmType);
                            }
                            else if (Helper.IsComplexType(edmType))
                            {
                                cspaceTypeName = ((ClrComplexType)edmType).CSpaceTypeName;
                                objectItemCollection._ocMapping.Add(cspaceTypeName, edmType);
                            }
                            else if (Helper.IsEnumType(edmType))
                            {
                                cspaceTypeName = ((ClrEnumType)edmType).CSpaceTypeName;
                                objectItemCollection._ocMapping.Add(cspaceTypeName, edmType);
                            }
                            // for the rest of the types like a relationship type, we do not have oc mapping,
                            // so we don't keep that information
                        }
                        catch (ArgumentException e)
                        {
                            throw new MappingException(Strings.Mapping_CannotMapCLRTypeMultipleTimes(cspaceTypeName), e);
                        }
                    }

                    // Create a new ObjectItemCollection and add all the global items to it.
                    // Also copy all the existing items from the existing collection
                    objectItemCollection.AtomicAddRange(globalItems);
                }

                // Update the value of known assemblies
                objectItemCollection._knownAssemblies = knownAssemblies;

                foreach (var loadedAssembly in knownAssemblies.Assemblies)
                {
                    CollectIfViewGenAssembly(loadedAssembly);
                }

                return(typesInLoading.Count != 0);
            }
        }
Exemple #9
0
        private bool LoadAssemblyFromCache(
            Assembly assembly,
            bool loadReferencedAssemblies,
            EdmItemCollection edmItemCollection,
            Action <string> logLoadMessage)
        {
            if (this.OSpaceTypesLoaded)
            {
                return(true);
            }
            if (edmItemCollection != null)
            {
                ReadOnlyCollection <EntityContainer> items = edmItemCollection.GetItems <EntityContainer>();
                if (items.Any <EntityContainer>() && items.All <EntityContainer>((Func <EntityContainer, bool>)(c => c.Annotations.Any <MetadataProperty>((Func <MetadataProperty, bool>)(a =>
                {
                    if (a.Name == "http://schemas.microsoft.com/ado/2013/11/edm/customannotation:UseClrTypes")
                    {
                        return(((string)a.Value).ToUpperInvariant() == "TRUE");
                    }
                    return(false);
                })))))
                {
                    lock (this.LoadAssemblyLock)
                    {
                        if (!this.OSpaceTypesLoaded)
                        {
                            new CodeFirstOSpaceLoader((CodeFirstOSpaceTypeFactory)null).LoadTypes(edmItemCollection, this);
                        }
                        return(true);
                    }
                }
            }
            KnownAssemblyEntry entry;

            if (this._knownAssemblies.TryGetKnownAssembly(assembly, this._loaderCookie, edmItemCollection, out entry))
            {
                if (!loadReferencedAssemblies)
                {
                    return(entry.CacheEntry.TypesInAssembly.Count != 0);
                }
                if (entry.ReferencedAssembliesAreLoaded)
                {
                    return(true);
                }
            }
            lock (this.LoadAssemblyLock)
            {
                if (this._knownAssemblies.TryGetKnownAssembly(assembly, this._loaderCookie, edmItemCollection, out entry) && (!loadReferencedAssemblies || entry.ReferencedAssembliesAreLoaded))
                {
                    return(true);
                }
                KnownAssembliesSet           knownAssemblies = new KnownAssembliesSet(this._knownAssemblies);
                Dictionary <string, EdmType> typesInLoading;
                List <EdmItemError>          errors;
                AssemblyCache.LoadAssembly(assembly, loadReferencedAssemblies, knownAssemblies, edmItemCollection, logLoadMessage, ref this._loaderCookie, out typesInLoading, out errors);
                if (errors.Count != 0)
                {
                    throw EntityUtil.InvalidSchemaEncountered(Helper.CombineErrorMessage((IEnumerable <EdmItemError>)errors));
                }
                if (typesInLoading.Count != 0)
                {
                    this.AddLoadedTypes(typesInLoading);
                }
                this._knownAssemblies = knownAssemblies;
                return(typesInLoading.Count != 0);
            }
        }
        private bool LoadAssemblyFromCache(
            Assembly assembly, bool loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action <String> logLoadMessage)
        {
            _viewAssemblyCache.CheckAssembly(assembly, loadReferencedAssemblies);

            // Code First already did type loading
            if (OSpaceTypesLoaded)
            {
                return(true);
            }

            // Check if its loaded in the cache - if the call is for loading referenced assemblies, make sure that all referenced
            // assemblies are also loaded
            KnownAssemblyEntry entry;

            if (_knownAssemblies.TryGetKnownAssembly(assembly, _loaderCookie, edmItemCollection, out entry))
            {
                // Proceed if only we need to load the referenced assemblies and they are not loaded
                if (loadReferencedAssemblies == false)
                {
                    // don't say we loaded anything, unless we actually did before
                    return(entry.CacheEntry.TypesInAssembly.Count != 0);
                }
                else if (entry.ReferencedAssembliesAreLoaded)
                {
                    // this assembly was part of a all hands reference search
                    return(true);
                }
            }

            lock (LoadAssemblyLock)
            {
                // Check after acquiring the lock, since the known assemblies might have got modified
                // Check if the assembly is already loaded. The reason we need to check if the assembly is already loaded, is that
                if (_knownAssemblies.TryGetKnownAssembly(assembly, _loaderCookie, edmItemCollection, out entry))
                {
                    // Proceed if only we need to load the referenced assemblies and they are not loaded
                    if (loadReferencedAssemblies == false ||
                        entry.ReferencedAssembliesAreLoaded)
                    {
                        return(true);
                    }
                }

                Dictionary <string, EdmType> typesInLoading;
                List <EdmItemError>          errors;
                var knownAssemblies = new KnownAssembliesSet(_knownAssemblies);

                // Load the assembly from the cache
                AssemblyCache.LoadAssembly(
                    assembly, loadReferencedAssemblies, knownAssemblies, edmItemCollection, logLoadMessage,
                    ref _loaderCookie, out typesInLoading, out errors);

                // Throw if we have encountered errors
                if (errors.Count != 0)
                {
                    throw EntityUtil.InvalidSchemaEncountered(Helper.CombineErrorMessage(errors));
                }

                // We can encounter new assemblies, but they may not have any time in them
                if (typesInLoading.Count != 0)
                {
                    // No errors, so go ahead and add the types and make them readonly
                    // The existence of the loading lock tells us whether we should be thread safe or not, if we need
                    // to be thread safe, then we need to use AtomicAddRange. We don't need to actually use the lock
                    // because the caller should have done it already
                    // Recheck the assemblies added, another list is created just to match up the collection type
                    // taken in by AtomicAddRange()
                    AddLoadedTypes(typesInLoading);
                }

                // Update the value of known assemblies
                _knownAssemblies = knownAssemblies;

                return(typesInLoading.Count != 0);
            }
        }