11

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?

Gerald Davis
  • 4,541
  • 2
  • 31
  • 47

4 Answers4

4

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.

mrahhal
  • 3,322
  • 1
  • 22
  • 41
3

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.

tugberk
  • 57,477
  • 67
  • 243
  • 335
  • Best answer so far. It looks like you are right. It is possible that those ps scripts are incompatible as the package, config and folder structure are changed. Migrate.exe is a dead end. It looks like it is only used to make changes to the database from the migration. Enable-Migration and Add-Migration are purely in powershell. Honestly all I would need is some standalone utility but for what I am doing now "DropCreateDatabaseAlways" initializer works fine. – Gerald Davis May 13 '15 at 21:11
  • 2
    There is also an issue with the commands and VS2015 RC in general: https://github.com/aspnet/EntityFramework/issues/1950. If you split your EF6 work out to a separate assembly and use the old csproj style, you should be able to reference it in your DNX451 app and get the commands to work (assuming you use the workaround in the link) – NPNelson May 14 '15 at 13:54
2

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.

Gillardo
  • 9,518
  • 18
  • 73
  • 141
-4

In asp.net 5 project you have to use entity framework 7 and use the ef migration command from command line.

I just done a recap here How can I manage EF 6 migrations in visual studio 2015?

Community
  • 1
  • 1
Luca Morelli
  • 2,530
  • 3
  • 28
  • 45
  • Your answer there as well as here is incorrect. Asp.Net 5 supports EF6. I have it working right now. The issue is limited to solely to support for migrations which is currently not supported via package manager (powershell). – Gerald Davis May 13 '15 at 20:57
  • I replied in the other question: I seen multple reports of incompatibility with identity and di mechanism that is part of asp.net 5. I think you can use it at you own risk, – Luca Morelli May 13 '15 at 20:59
  • Well the entire stack is pre-release so it is all use at your own risk. I think some of the confusion is that asp.net 5 != core clr 5.0. ASP.NET 5 can run using EITHER core clr 5.0 or full framework 4.5.1. Of course Microsoft is horrible about naming things so it gets confusing but core clr 5 does NOT support EF6. It lacks some of the required system assemblies which EF6 uses. 4.5.1 does support EF6. ASP.NET 5 can run on either. So it is possible to have EF6 and ASP.NET 5 in the same project as long as it only targets clr 4.5.1. not core clr 5.0. – Gerald Davis May 13 '15 at 21:05
  • I don't say use at your own risk because is pre release, but because has been done the choose to move to ef 7, and it's also available for traditional .net apps. I say use at your own risk because it's not a supported scenario, so you can say your application stop to run after what ever update of dnx and/or asp.net 5 packages – Luca Morelli May 13 '15 at 21:10
  • 1
    Here is a link that describes how ASPNET5 supports running EF6 on dnx451. https://msdn.microsoft.com/en-us/magazine/dn973011.aspx .If you want to target DNXCore50 then you have to use EF7. As much as I want to convert my apps to CoreCLR, I am finding EF7 to be too rough around the edges right now for most of my uses. I am refactoring a lot of my libraries to support CoreCLR, but for now, any library that depends on EF will remain at EF6, which means it can't target CoreCLR. Once I am comfortable with EF7, it should be an easy switch to CoreCLR – NPNelson May 14 '15 at 13:30
  • @NPNelson That articles covers everything except migrations. Migrations in EF6 relies on powershell and that integration appears to be broken in xproj projects. – Gerald Davis May 14 '15 at 19:09