Can't enable migrations for Entity Framework o

2020-03-01 06:44发布

问题:

I just installed VS 2017 and created a new Core project. Inside it, I added:

  • Microsoft.EntityFrameworkCore (1.1.0)
  • Microsoft.NETCore.App (1.1.0)

I also created a folder called Models with a class in it. Then, I went to the Package Manager Console and executed enable-migrations so that VS will create the files needed for me. However, instead of the needed files, I got the following error.

The term 'enable-migration' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

What am I supposed to do here?

I seem to be out of luck googling. It's a very unusual combination (Core VS17 and EF) so there's virtually no info on the subject. Some guides talk about editing package.json by hand which is kind of hard because VS 2017 seems to upgrade the Core so that dotnet restore doesn't look for it but rather for a xxx.csproj.

In summary, I'm confused and quite uncertain how to bite the problem.

回答1:

EF Core does not have Enable-Migrations command anymore. Migrations are enabled "by default". Add new migration with Add-Migration.

Documentation about package manager commands is here



回答2:

I had a similar issue with Visual Studio 2017 and a imported project from VS 2015, EF Migrations was no longer working.

After reading through the documentation on GitHub: https://github.com/aspnet/EntityFramework/issues/7031

I installed through Nuget this package:

"Microsoft.EntityFrameworkCore"

It caused a cascade effect of multiple downloads/installs. After the install, and restarting VS 2017, Everything is now back to normal.

Two days later, with another Visual Studio 2015 project, I had the same issue, and resolved it following the above procedure, but then I got this new error:

"Inheritance security rules violated by type: 'System.Net.Http.WebRequestHandler'. Derived types must either match the security accessibility of the base type or be less accessible."

I resolved this error by upgrading the project Framework to the latest ".NET Framework 4.6.2" (Scary.... but it worked without any further issues)

I referred to this documentation on GitHub: https://github.com/dotnet/corefx/issues/11100



回答3:

I just installed Microsoft.EntityFrameworkCore.Tools, then I tried to execute enable-migrations then I got the following message in VS2017:

Enable-Migrations is obsolete. Use Add-Migration to start using Migrations.

Hope it helps.



回答4:

As others mentioned before, enable migrations is obsolete and is turned on anyway, and you can use the Add-Migration InitialCreate.

I'll add that you do NOT need to add more packages to the project, as it will already reference the necessary assemblies in the Microsoft.AspNetCore.All metapackage.

You can also use your console (cmd/shell depending on your OS) instead of the PM power shell console. To do so, you need to add a reference to the dotnet tool: edit your project file (right click it and select 'edit .csproj' - and add the following reference in an ItemGroup:

<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0"></DotNetCliToolReference>

and then in the console type dotnet ef migrations add InitialCreate

Add Migration in .NET Core

.NET Core CLI



回答5:

Resolved

  1. Right click on the Project, then click on the option: Manage NuGet Package; in Browse, look for the EntityFramework add-on, install it, then restart Visual Studio.
  2. Create a MyDbContext class that inherits from DbContext.
  3. In Tools> NuGet Package Manager> Package Manager Console
  4. Enable-Migrations
  5. add-migration InitialModel


回答6:

go to Tools->NuGet Package Manager->Package Manager Console

write

install-package EntityFramework

then

enable-migrations

this command runs for whole lifetime of a project means you have to write it only once in a project however if we change model or add another class or whatever change we can use this command

add-migration migrationNameTest

'migrationNameTest' is a name of migration which you want to assign to it however to run the migration go to package manager and run

update-database