示例#1
0
        private KeyValuePair <Type, Func <DbContext> > FindContextType(string name)
        {
            var types = FindContextTypes();

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

                throw new OperationException(DesignCoreStrings.MultipleContexts);
            }

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

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

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

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

            return(candidates.First());
        }