Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable migrations (EF6) in an asp.net 5 project?

I created a new class library (package) project (prior to VS 2015 RC used the even worse name of asp.net class library to represent the data layer. Just to be clear this is the newer kproj style structure.

Added EF 6.1.3 to project.json. Currently only targeting DNX451.

   "dependencies": {
        "EntityFramework": "6.1.3"
        ,"Moq": "4.2.1502.911"
    },

Created initial model classes and using a AlwaysCreate database initializer everything works fine. Now need to switch to migrations so used Enable-Migrations in the package manager console and got:

Enable-Migrations : The term 'Enable-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.
At line:1 char:1
+ Enable-Migrations
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Enable-Migrations:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

For EF7 migrations the package manager is not supported for migration commands. Instead there is a new ef command run through dnu but that new process is just for EF7 not EF6 right?

Why does package manager think Enable-Migrations is invalid even though EF6 has been referenced?

like image 280
Gerald Davis Avatar asked May 13 '15 16:05

Gerald Davis


3 Answers

EDIT for 1.0.0: Migrator.EF6 now supports 1.0.0.


EDIT for RC2: Migrator.EF6 now supports RC2.


What we need is a dnx command line tool that wraps EF6 and delegates commands to it. That's because DNX projects won't see init.ps1 and install.ps1 that are needed to get Add-Migration to work (at least for now, see this).

The implementation isn't that hard but no one seems to have given it time so I ended up doing one.

This solution doesn't require a separate .csproj, the same workflow happens but instead of Update-Database you'll do dnx ef database update and similar commands.

Instructions and samples can be found here: Migrator.EF6.

Later on, probably in RTM, you'll be able to use these in DNX projects as well. And that's probably why Microsoft hasn't made one to support EF6 migrations.

like image 116
mrahhal Avatar answered Nov 17 '22 21:11

mrahhal


Why does package manager think Enable-Migrations is invalid even though EF6 has been referenced?

Because I assume and am quite sure that ASP.NET 5 projects don't invoke install and uninstall PowerShell scripts inside the packages which EF 6 package has to add the migration commands to package manager console. Your best luck is to try to integrate the command line tool (I believe it is called migrate.exe, not sure) inside the EF6 NuGet package.

like image 3
tugberk Avatar answered Nov 17 '22 20:11

tugberk


I have managed to get round this by creating an old .csproj project which the Enable-Migrations command works. The only difference is that instead of having a separate copy of the files, i have added them all by reference, using the "Add By Link" option when adding an existing file.

Now i can add migrations etc and change the files as needed, and these changes will automatically reflect in my .xproj as it is the same files.

like image 2
Gillardo Avatar answered Nov 17 '22 20:11

Gillardo