Efcore: No access to MigrationsScaffolder in 3.0.0-preview7.19362.6 anymore

Created on 31 Jul 2019  路  3Comments  路  Source: dotnet/efcore

For some reason the namespaces Microsoft.EntityFrameworkCore.Migrations.Design and Microsoft.EntityFrameworkCore.Migrations.Internal are not available anymore in version 3.0.0-preview7.19362.6. But I need access to the MigrationsScaffolder to dynamically generate migration classes at runtime.

My current workaround is to download the EntityFrameworkCore source code, build the EFCore.Design project and reference the Microsoft.EntityFrameworkCore.Design.dll directly.

Steps to reproduce

Install the following nuget packages:

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.Relational

Try the code below (similar as in #10872)

```c#
public static class DbContextExtensions
{
private const string SubNamespace = "Migrations";

public static ScaffoldedMigration ScaffoldMigration(this DbContext context, string migrationName, string rootNamespace)
{
    if (context == null)
    {
        throw new ArgumentNullException(nameof(context));
    }

    if (migrationName == null)
    {
        throw new ArgumentNullException(nameof(migrationName));
    }

    if (rootNamespace == null)
    {
        throw new ArgumentNullException(nameof(rootNamespace));
    }

    var logger = context.GetService<ILoggerFactory>()
        .CreateLogger($"{rootNamespace}.{SubNamespace}");

    var reporter = new OperationReporter(
        new OperationReportHandler(
            m => logger.LogError(m),
            m => logger.LogWarning(m),
            m => logger.LogInformation(m),
            m => logger.LogTrace(m)));

    var designTimeServices = new ServiceCollection()
        .AddSingleton(context.GetService<IHistoryRepository>())
        .AddSingleton(context.GetService<IMigrationsIdGenerator>())
        .AddSingleton(context.GetService<IMigrationsModelDiffer>())
        .AddSingleton(context.GetService<IMigrationsAssembly>())
        .AddSingleton(context.Model)
        .AddSingleton(context.GetService<ICurrentDbContext>())
        .AddSingleton(context.GetService<IDatabaseProvider>())
        .AddSingleton(context.GetService<IMigrator>())
        .AddSingleton(context.GetService<IRelationalTypeMappingSource>())
        .AddSingleton<IMigrationsCodeGeneratorSelector, MigrationsCodeGeneratorSelector>()
        .AddSingleton<MigrationsCodeGeneratorDependencies>()
        .AddSingleton<ICSharpHelper, CSharpHelper>()
        .AddSingleton<CSharpMigrationOperationGeneratorDependencies>()
        .AddSingleton<ICSharpMigrationOperationGenerator, CSharpMigrationOperationGenerator>()
        .AddSingleton<CSharpSnapshotGeneratorDependencies>()
        .AddSingleton<ICSharpSnapshotGenerator, CSharpSnapshotGenerator>()
        .AddSingleton<CSharpMigrationsGeneratorDependencies>()
        .AddSingleton<IMigrationsCodeGenerator, CSharpMigrationsGenerator>()
        .AddSingleton<IOperationReporter>(reporter)
        .AddSingleton<MigrationsScaffolderDependencies>()
        .AddSingleton<MigrationsScaffolder>()
        .AddSingleton<ISnapshotModelProcessor, SnapshotModelProcessor>()
        .BuildServiceProvider();

    var scaffolder = designTimeServices.GetRequiredService<MigrationsScaffolder>();

    return scaffolder.ScaffoldMigration(migrationName, rootNamespace, SubNamespace);
}

}
```

Further technical details

EF Core version: 3.0.0-preview7.19362.6
Operating system: Windows 10
IDE: Visual Studio 2019 16.1.6

closed-question customer-reported

Most helpful comment

All 3 comments

I had same issue, please share your .csproj file

Thanks @smitpatel.

It works with the following package reference entry in the .csproj file:

<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0-preview7.19362.6">
    <PrivateAssets>all</PrivateAssets>
</PackageReference>

Instead of this:

<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0-preview7.19362.6">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Was this page helpful?
0 / 5 - 0 ratings