private Type FindContextType(string name)
        {
            var types = GetContextTypes();

            Type[] candidates;

            if (string.IsNullOrEmpty(name))
            {
                candidates = types.Take(2).ToArray();
                if (candidates.Length == 0)
                {
                    throw new OperationException(CommandsStrings.NoContext);
                }
                if (candidates.Length == 1)
                {
                    return(candidates[0]);
                }

                throw new OperationException(CommandsStrings.MultipleContexts);
            }

            candidates = FilterTypes(types, name, ignoreCase: true).ToArray();
            if (candidates.Length == 0)
            {
                throw new OperationException(CommandsStrings.NoContextWithName(name));
            }
            if (candidates.Length == 1)
            {
                return(candidates[0]);
            }

            // Disambiguate using case
            candidates = FilterTypes(candidates, name).ToArray();
            if (candidates.Length == 0)
            {
                throw new OperationException(CommandsStrings.MultipleContextsWithName(name));
            }
            if (candidates.Length == 1)
            {
                return(candidates[0]);
            }

            // Allow selecting types in the default namespace
            candidates = candidates.Where(t => t.Namespace == null).ToArray();
            if (candidates.Length == 0)
            {
                throw new OperationException(CommandsStrings.MultipleContextsWithQualifiedName(name));
            }

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

            return(candidates[0]);
        }
예제 #2
0
        private KeyValuePair <Type, Func <DbContext> > FindContextType(string name)
        {
            var types = FindContextTypes();

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

                throw new OperationException(CommandsStrings.MultipleContexts);
            }

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

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

            // Disambiguate using case
            candidates = FilterTypes(candidates, name);
            if (candidates.Count == 0)
            {
                throw new OperationException(CommandsStrings.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(CommandsStrings.MultipleContextsWithQualifiedName(name));
            }

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

            return(candidates.First());
        }