Note: original issue below, but renamed the title. I'm really just looking for a way of generation migrations programatically.
On nightlies (1.1.0-alpha1-*). Attempt to generate a new migration.
Just setting up this project on a new laptop. Have done a dotnet restore, build etc.
Note, I am invoking the tool within my own wrapper - to supply it with custom config. For this, I reference:
"Microsoft.EntityFrameworkCore.Design": {
"type": "build",
"version": "1.0.0-preview2-final"
}
Receive this exception:
System.MissingMethodException: Method not found: 'System.String Microsoft.EntityFrameworkCore.Metadata.Internal.EntityTypeExtensions.DisplayName(Microsoft.EntityFrameworkCore.Metadata.IEntityType)'.
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypeAnnotations(IEntityType entityType, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityType(String builderName, IEntityType entityType, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypes(String builderName, IReadOnlyList`1 entityTypes, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.Generate(String builderName, IModel model, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateMetadata(String migrationNamespace, Type contextType, String migrationName, String migrationId, IModel targetModel)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace)
at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.Execute(CommonOptions commonOptions, String name, String outputDir, String context, String environment, Action`1 reporter)
at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.<>c__DisplayClass0_0.<Configure>b__0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
I've since updated Microsoft.EntityFrameworkCore.Design to 1.1.0-alpha1-*, but now the Microsoft.EntityFrameworkCore.Tools namespace is gone. Where can I find it? The Microsoft.EntityFrameworkCore.Tools package (my best guess) is still on 1.0.0-preview3-*, which doesn't seem to have it.
I appreciate this is internal, likely-to-break stuff. but there were several sticky bugs in 1.0, and while I was pinned to a specific nightly build for a while, it looks like they are cycled after a few days.. so I'm hoping this quick question which I couldn't solve after a good hour's hunt won't be too much trouble to answer? Sorry!
Ok great - I found "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-preview3-*" snooping through some build files in the new aspnet/EntityFramework.Tools repo.
I now recieve:
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.DotNet.InternalAbstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
I can't think how this might happen - but it doesn't seem to be a dependency of the new package.
Maybe I was mistaken.. invoking Microsoft.EntityFrameworkCore.Tools.DotNet.Program.Main in the same way as before now invokes dotnet run on my assembly, or at least a build - while it's already running - and then tries to overwrite it.
Would this mean my directly referencing InternalAbstractions is working? - Is this just something new on migrations gen code?
Is there some other way of generating migrations in code so I can avoid this?
Apologies for making this issue a bit of a journey. I think after all I just want to find Microsoft.EntityFrameworkCore.Tools.Program (from https://github.com/aspnet/EntityFramework.Tools/blob/dev/src/ef/Program.cs) again.
dotnet-ef/Microsoft.EntityFrameworkCore.Tools.DotNet is a wrapper over ef which is a wrapper over OperationExecutor in Microsoft.EntityFrameworkCore.Design.
The most direct way to generate migrations programmatically is like this using Microsoft.EntityFrameworkCore.Design.
``` C#
using (var db = new MyDbContext())
{
var services = ((IInfrastructure
var codeHelper = new CSharpHelper();
var scaffolder = ActivatorUtilities.CreateInstance
services,
new CSharpMigrationsGenerator(
codeHelper,
new CSharpMigrationOperationGenerator(codeHelper),
new CSharpSnapshotGenerator(codeHelper)));
var migration = scaffolder.ScaffoldMigration(
"MyMigration",
"MyApp.Data");
File.WriteAllText(
migration.MigrationId + migration.FileExtension,
migration.MigrationCode);
File.WriteAllText(
migration.MigrationId + ".Designer" + migration.FileExtension,
migration.MetadataCode);
File.WriteAllText(
migration.SnapshotName + migration.FileExtension,
migration.SnapshotCode);
}
```
That's fantastic, thank you @bricelam. I've been doing a passthrough of sorts to bootstrap the existing tool, so I get complete coverage of all of the ef commands, but get the chance to run in exactly the same context as the main app, with all the config startup code (not easily possible just using a custom context factory). Ill fall back to this one, and can dig around for how to apply the migrations too.
Ideally I'd still be able to invoke them all and just pass through - but I just can't find Microsoft.EntityFrameworkCore.Tools.Program in any nuget package published. Am I just looking in the wrong place?
To be specific: that class exists in 1.0.0-preview2-* of Microsoft.EntityFrameworkCore.Tools, but not 1.0.0-preview3-* (or preview4, which was uploaded a few hours ago).
We dramatically changed the architecture of how tools work in #6288. A lot of things got moved around after Preview 2.
Applying Migrations is even easier:
C#
using (var db = new MyDbContext())
{
db.Database.Migrate();
}
Great thank you, I will review. Very much appreciate your time, thanks again.
@bricelam , could you update the snippet about creating MigrationsScaffolder for EF Core 2.0?
See #9339
Most helpful comment
See #9339