Enabling Migrations in EF core?

♀尐吖头ヾ 提交于 2019-12-05 18:31:30

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.

Edit your .csproj where you have EF Core 2.0 and add:

<ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
  1. Open Windows PowerShell
  2. Go to directory where you have EF Core 2.0
  3. Type dotnet ef migrations add <<migration's_name>>. For instance: dotnet ef migrations add Init. If your startup project is in different folder then you can use --startup-project ../<<other_project_folder>>

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

This enables the migration.


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


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
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!