Enabling Migrations in EF core?

in powershell CLI type this --> dotnet ef migrations add InitialMigration

This enables the migration.enter image description here


This will install the correct core tools

// Package Manger
PM> Install-Package Microsoft.EntityFrameworkCore.Tools -Version 2.0.1
// or this will work inside the CLI Console
dotnet add package Microsoft.EntityFrameworkCore.Tools --version 2.0.1
// **for the current/LATEST ver. leave out version option it will install latest Install-Package Microsoft.EntityFrameworkCore.Tools 

enter image description here


Fixing your bug issue:

Look at this SO answer: "You should just need to update the tools section of your project.json file to include this:"

"Microsoft.EntityFrameworkCore.Tools": {
  "version": "2.0.1",  // I corrected this from previous answer for your version
  "imports": [
    "portable-net45+win8+dnxcore50",
    "portable-net45+win8"
  ]
}

Bonus :) To run migrations automatically... in startup.cs of your main application.

// setup the HTTP request pipeline to check and migrate.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{           
    try
    {
        using (var migrationSvcScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
            .CreateScope())
        {
         migrationSvcScope.ServiceProvider.GetService<EFMigrationsMyDBContext>().Database.Migrate();
            // you can also add the data here... let me know if you need I will post it
        }
    }   
    ... // Rest of the startup stuff
}

Go to the Package Manager Console and install the needed tools with Install-Package Microsoft.EntityFrameworkCore.Tools. When it has completed try to use the command EntityFrameworkCore\Add-Migration firstMigration.

Tags:

C#

Ef Core 2.0