public virtual Type TryFindContextType( Assembly assemblyHint, Type contextTypeHint, IEnumerable<Type> typesToSearch = null) { if (contextTypeHint != null) { return contextTypeHint; } // If no context type is known then try to find a single DbContext in the given assembly that // is attributed with the DbConfigurationTypeAttribute. This is a heuristic for tooling such // that if tooling only knows the assembly, but the assembly has a DbContext type in it, and // that DbContext type is attributed, then tooling will use the configuration specified in that // attribute. var contextTypes = (typesToSearch ?? assemblyHint.GetAccessibleTypes()) .Where( t => t.IsSubclassOf(typeof(DbContext)) && !t.IsAbstract && !t.IsGenericType && t.GetCustomAttributes(typeof(DbConfigurationTypeAttribute), inherit: true).Any()) .ToList(); return contextTypes.Count == 1 ? contextTypes[0] : null; }
public MigrationAssembly(Assembly migrationsAssembly, string migrationsNamespace) { Contract.Requires(migrationsAssembly != null); _migrations = (from t in migrationsAssembly.GetAccessibleTypes() where t.IsSubclassOf(typeof(DbMigration)) && typeof(IMigrationMetadata).IsAssignableFrom(t) && t.GetConstructor(Type.EmptyTypes) != null && !t.IsAbstract && !t.IsGenericType && t.Namespace == migrationsNamespace select (IMigrationMetadata)Activator.CreateInstance(t)) .Where(mm => !string.IsNullOrWhiteSpace(mm.Id) && mm.Id.IsValidMigrationId()) .OrderBy(mm => mm.Id) .ToList(); }
public virtual Type TryFindConfigurationType( Assembly assemblyHint, Type contextTypeHint, IEnumerable<Type> typesToSearch = null) { DebugCheck.NotNull(assemblyHint); if (contextTypeHint != null) { var typeFromAttribute = contextTypeHint.GetCustomAttributes(inherit: true) .OfType<DbConfigurationTypeAttribute>() .Select(a => a.ConfigurationType) .FirstOrDefault(); if (typeFromAttribute != null) { if (!typeof(DbConfiguration).IsAssignableFrom(typeFromAttribute)) { throw new InvalidOperationException( Strings.CreateInstance_BadDbConfigurationType(typeFromAttribute.ToString(), typeof(DbConfiguration).ToString())); } return typeFromAttribute; } } var configurations = (typesToSearch ?? assemblyHint.GetAccessibleTypes()) .Where( t => t.IsSubclassOf(typeof(DbConfiguration)) && !t.IsAbstract && !t.IsGenericType) .ToList(); if (configurations.Count > 1) { throw new InvalidOperationException( Strings.MultipleConfigsInAssembly(configurations.First().Assembly, typeof(DbConfiguration).Name)); } return configurations.FirstOrDefault(); }