예제 #1
0
        public virtual ScaffoldedMigration AddMigration(
            [NotNull] string migrationName,
            [NotNull] string rootNamespace,
            [CanBeNull] string contextTypeName)
        {
            Check.NotEmpty(migrationName, "migrationName");
            Check.NotEmpty(rootNamespace, "rootNamespace");

            var contextType = GetContextType(contextTypeName);

            using (var context = CreateContext(contextType))
            {
                var scopedServiceProvider = ((IDbContextServices)context).ScopedServiceProvider;
                var options = scopedServiceProvider.GetRequiredService <DbContextService <IDbContextOptions> >();
                var model   = scopedServiceProvider.GetRequiredService <DbContextService <IModel> >();

                var migrator   = CreateMigrator(context);
                var scaffolder = new MigrationScaffolder(
                    context,
                    options.Service,
                    model.Service,
                    migrator.MigrationAssembly,
                    migrator.ModelDiffer,
                    new CSharpMigrationCodeGenerator(new CSharpModelCodeGenerator()));

                return(scaffolder.ScaffoldMigration(migrationName, rootNamespace));
            }
        }
        public virtual ScaffoldedMigration CreateMigration(
            [NotNull] string migrationName,
            [NotNull] string rootNamespace,
            [CanBeNull] string contextTypeName)
        {
            Check.NotEmpty(migrationName, "migrationName");
            Check.NotEmpty(rootNamespace, "rootNamespace");

            var contextType = GetContextType(contextTypeName);

            using (var context = CreateContext(contextType))
            {
                var configuration = context.Configuration;

                var extension = RelationalOptionsExtension.Extract(configuration);
                if (extension.MigrationNamespace == null)
                {
                    extension.MigrationNamespace = rootNamespace + ".Migrations";
                }

                var serviceProvider = configuration.Services.ServiceProvider;
                var scaffolder      = new MigrationScaffolder(
                    configuration,
                    serviceProvider.GetService <MigrationAssembly>(),
                    serviceProvider.GetService <ModelDiffer>(),
                    new CSharpMigrationCodeGenerator(new CSharpModelCodeGenerator()));

                var migration = scaffolder.ScaffoldMigration(migrationName);

                // Derive default directory from namespace
                if (migration.Directory == null)
                {
                    var directory = migration.MigrationNamespace;
                    if (directory.StartsWith(rootNamespace + '.'))
                    {
                        directory = directory.Substring(rootNamespace.Length + 1);
                    }

                    migration.Directory = directory.Replace('.', Path.DirectorySeparatorChar);
                }

                return(migration);
            }
        }