Skip to content

Automatic Migrations #6214

Not planned
Not planned
@netcore-jroger

Description

@netcore-jroger

Usually we use EF tool to migrate the changes. but I want to know how to the using C# code auto migrate the change, and do not use EF tool everytimes.

Is there any way to do it ?
Thanks!

Activity

netcore-jroger

netcore-jroger commented on Aug 2, 2016

@netcore-jroger
Author

@Muchiachio My code is like what you said above.
However, you still need to use the command dotnet ef migrate add migrate_name to generate snapshot files .
And I dot not want to use dotnet CLI tool. just want use C# code in my application start.
Is there any way to do it ?
Thanks !

bjorn-ali-goransson

bjorn-ali-goransson commented on Aug 2, 2016

@bjorn-ali-goransson

I can imagine that the logic required to generate the migration procedure doesn't event exist inside EF Core itself, but inside that second NuGet package (so using it automatically may be hard?).

Maybe using Roslyn, one could compile it dynamically ... but then again, what would happen with the EfCore Migrations table ...

bjorn-ali-goransson

bjorn-ali-goransson commented on Aug 2, 2016

@bjorn-ali-goransson

In my view, this feature request (if not already available!) should be directed to the Tools-package. Some method could be defined on the classes there. I read that the auto-migrate feature was not included by design, though, so don't count on it being implemented...

added this to the 1.1.0 milestone on Aug 2, 2016
bricelam

bricelam commented on Aug 9, 2016

@bricelam
Contributor

@Oger-Me Are you trying to implement automatic migrations (like we had in EF6) where no Migration class is scaffolded and stored in the project? Or is your goal to scaffold the Migration class without using the Add-Migration/dotnet ef migrations add command?

bricelam

bricelam commented on Aug 9, 2016

@bricelam
Contributor

To generate a migrations file, the logic lives inside the Microsoft.EntityFrameworkCore.Design package:

using (var context = new MyDbContext())
{
    var services = ((IInfrastructure<IServiceProvider>)context).Instance;
    var codeHelper = new CSharpHelper();
    var scaffolder = ActivatorUtilities.CreateInstance<MigrationsScaffolder>(
        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);
}
bricelam

bricelam commented on Aug 9, 2016

@bricelam
Contributor

Implementing automatic migrations is a lot more involved, but I'd be happy to give you the high-level steps if your interested.

removed this from the 1.1.0 milestone on Aug 9, 2016
added this to the Discussions milestone on Aug 10, 2016

110 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jzabroski@lakeman@bjorn-ali-goransson@cocowalla@bricelam

        Issue actions

          Automatic Migrations · Issue #6214 · dotnet/efcore