Ejemplo n.º 1
0
        private KeyValuePair <Type, Func <DbContext> > FindContextType(string name)
        {
            var types = FindContextTypes();

            if (string.IsNullOrEmpty(name))
            {
                if (types.Count == 0)
                {
                    throw new OperationException(ToolsCoreStrings.NoContext);
                }
                if (types.Count == 1)
                {
                    return(types.First());
                }

                throw new OperationException(ToolsCoreStrings.MultipleContexts);
            }

            var candidates = FilterTypes(types, name, ignoreCase: true);

            if (candidates.Count == 0)
            {
                throw new OperationException(ToolsCoreStrings.NoContextWithName(name));
            }
            if (candidates.Count == 1)
            {
                return(candidates.First());
            }

            // Disambiguate using case
            candidates = FilterTypes(candidates, name);
            if (candidates.Count == 0)
            {
                throw new OperationException(ToolsCoreStrings.MultipleContextsWithName(name));
            }
            if (candidates.Count == 1)
            {
                return(candidates.First());
            }

            // Allow selecting types in the default namespace
            candidates = candidates.Where(t => t.Key.Namespace == null).ToDictionary(t => t.Key, t => t.Value);
            if (candidates.Count == 0)
            {
                throw new OperationException(ToolsCoreStrings.MultipleContextsWithQualifiedName(name));
            }

            Debug.Assert(candidates.Count == 1, "candidates.Count is not 1.");

            return(candidates.First());
        }