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.
Install the following nuget packages:
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);
}
}
```
EF Core version: 3.0.0-preview7.19362.6
Operating system: Windows 10
IDE: Visual Studio 2019 16.1.6
I had same issue, please share your .csproj file
https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#microsoftentityframeworkcoredesign-is-now-a-developmentdependency-package
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>
Most helpful comment
https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#microsoftentityframeworkcoredesign-is-now-a-developmentdependency-package