Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
884 views
in Technique[技术] by (71.8m points)

entity framework 6 - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...