How can I manage EF 6 migrations in visual studio

2019-02-12 11:30发布

问题:

I started a new MVC project with EntityFramework -Version 6.1.2 using Visual Studio 2013 latest update. I made a couple of migrations and updated the database. After this I checked out the project on another computer and opened with Visual Studio 2015 CTP 6.

If I go in the package manager console and try to run any migration commands, they're not recognized:

add-migrations : The term 'add-migrations' 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.

How can I manage my migrations using Entity Framework 6 in Visual Studio 2015?

回答1:

Have you tried uninstalling and reinstalling EntityFramwork NuGet package? I had the same issue as described and it solved it.



回答2:

OK so I had this problem with an EF6 project I started in VS2013 and upgraded to VS2015 RC. The commands were available if I opened it in VS2013 but not in VS2015. What I found to solve the problem quickly and easily was to use the reinstall package command:

Update-Package –reinstall EntityFramework

This fixed any command missing errors and then allowed me to start database updates and create new migrations within VS2015.



回答3:

I have done a few checks, but I'll recap trying to cover all the ef scenario:

ef7 in an asp.net 5 project: the migrations management has been changed and now is handled using the ef migration command.
ef7 in a traditional .net application: it's required framework 4.5.1, but at this page esing ef 7 in a .net app you can find a good guide on how to use ef 7 in a traditional app. It's important to add the EntityFramework.Commands package.
ef6: doesn't work with asp.net 5, so even you can install it, the build will be broken. Same situation for most of the actual ef drivers as the ones for mongodb and Postgresql. If you're not sure is a ef 7 compatible driver, don't install it. At the moment I think you can count just on sqlserver and inMemory.
for asp.net 4 applications, the actual installation of the commands is part of the entity framework package itself, so will be installed at the first installation of the package (as rightly pointed out above).
In some situations, in an environment where ef package has not been installed, copying an asp.net 4 project and starting to work on it, may give this issue, that magically disappears when for whatever reason the package is installed.



回答4:

I was having similar issues in that I could not run Add-Migration from the Package Manager Console, either (though I was getting a different error.) Unfortunately, reinstalling EntityFramework was not working for me, perhaps because I had ASP.Net 5 projects in the same solution. (This was not for a traditional csproj, as it requires resx's which are not supported as far as I know.)

I ended up creating a console application, referencing the project that included my migrations, and added the following:

    var configuration = GetConfiguration();

    var scaffolder = new MigrationScaffolder(configuration);

    scaffolder.Namespace = configuration.MigrationsNamespace;

    var scaffoldedMigration = scaffolder.Scaffold(name);

    System.IO.File.WriteAllText(scaffoldedMigration.MigrationId + ".cs", scaffoldedMigration.UserCode);
    System.IO.File.WriteAllText(scaffoldedMigration.MigrationId + ".Designer.cs", scaffoldedMigration.DesignerCode);
    System.IO.File.WriteAllText(scaffoldedMigration.MigrationId + ".resx", BuildResx(scaffoldedMigration.Resources));

GetConfiguration returns an instance of your *.Migration.Configuration class. name is the new name of your migration. This code by default drops it into the Debug folder of your console app; drag-and-drop from there into Visual Studio in your Migration folder and it should do the trick.

I later discovered that this was basically the same as https://stackoverflow.com/a/20382226/195653 but for completely different reasons.



回答5:

I did all combinations of reinstall or uninstall and install EntityFramework plus closing and reopening VS. The only thing that worked was deleting the folder packages in my solution folder and rebuild the solution to force all packages to download.



回答6:

Update-Package EntityFramework ...should do the trick.