private static ContextKeySchemaInfo GetContextKeySchema(Assembly assembly, string configurationType)
{
var targetAssemblyConfig = (DbMigrationsConfiguration)assembly.CreateInstance(configurationType);
if (targetAssemblyConfig == null)
{
Log.Debug(
"Unable to create DbMigrationsConfiguration for {configurationType} using assembly {assembly}.",
configurationType,
assembly.CodeBase);
return null;
}
// modified from EF6 System.Data.Entity.Migrations.Infrastructure.MigrationAssembly constructor
// https://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework/Migrations/Infrastructure/MigrationAssembly.cs
var schemaNames =
from dt in assembly.DefinedTypes
let t = dt.AsType()
let ti = t.GetTypeInfo()
where
t.IsSubclassOf(typeof(DbMigration)) &&
typeof(IMigrationMetadata).IsAssignableFrom(t) &&
!ti.IsAbstract &&
!ti.IsGenericType &&
t.Namespace == targetAssemblyConfig.MigrationsNamespace
let defaultSchema = new ResourceManager(t).GetString("DefaultSchema")
select string.IsNullOrEmpty(defaultSchema) ? "dbo" : defaultSchema;
// we're only supporting one schema per configuration type
var schemaName = schemaNames.Distinct().Single();
return new ContextKeySchemaInfo(targetAssemblyConfig.ContextKey, schemaName);
}