How can I do an Entity Framework Core project and add a Migration in Visual Studio 2015 from nuget prompt?

11,901

Solution 1

You missed a step. From the documentation at the time that your beta 8 was active, the missing command would have been:

Install-Package EntityFramework.Commands -Pre to make the migrations commands available in your project.

The Commands were moved to a separate package, presumably to make the main framework more lightweight for production, where you can't use and don't need command line utilities.

Answer-Update-contributed-by-Warren: In Asp.net core 1.0.0-RC2 the relevant package is named Microsoft.EntityFrameworkCore.Tools and it appears to be part of the ASP.Net Core Web application template making the original problem highly unlikely to occur unless someone actually damages their project.json. Command line "EF" migration commands should not be run from Package Manager Console, which is NOT a general environment for DOTNET commands.

Solution 2

The following is only applicable to dnx which was part of ASP.NET Core RC1. This has been replaced and is not applicable part of ASP.NET Core.

Answering your questions regarding the better way you're looking for to run the dnx command:

My dnx command was also giving the error.

The term 'dnx' is not recognized as the name of a cmdlet, function,

in both my Visual Studio package manager console and Windows powershell.

After much frustration I was able to resolve it.

Here are the steps I did to get it working

  • Close all instances of Visual Studio
  • Delete the .dnx folder in C:\Users\YOURUSERNAME\.dnx. Mine was well over 1 GB
  • Open Visual Studio 2015. Create a new default template ASP.NET 5 Web Application. Build and run it. This will recreate the .dnx folder. Afterwards the folder size was nearly 600MB
  • Open Powershell
  • Navigate to the .dnx bin directory

    cd\
    cd users\YOURUSERNAME\.dnx\bin
    
  • Run the command

    .\dnvm upgrade
    

    If you an error message "execution of scripts is disabled on this system." then run the following command to give you permission first

    Set-ExecutionPolicy RemoteSigned
    

Go Open Visual Studio. You will then be able to type the command dnx in both package manager console and powershell

   dnx

and it will recognize it

enter image description here

Share:
11,901
Warren  P
Author by

Warren P

Software Developer: Web, Desktop, and Server service developer JavaScript (ExtJS/SenchaTouch/jquery), HTML5, CSS C#/ASP.NET MVC/ASP.NET Core/Roslyn/Visual Studio 2015 Delphi/ObjectPascal C (Linux and others) and Objective-C (have an app in the app store) Python C++ Smalltalk/Pharo/Squeak Embedded Systems, and cross-platform stuff (Windows, Linux, Mac OS X) नमस्ते. मैं हिंदी फिल्मों से प्यार है. Bits of code at Bitbucket hosting: https://bitbucket.org/wpostma More bits of code at Github: https://github.com/wpostma

Updated on June 07, 2022

Comments

  • Warren  P
    Warren P almost 2 years

    In Visual Studio 2015, I created an ASP.net Core project (formerly called ASP.NET 5). The template for the project creates an MS SQL localdb for persistence, along with entity framework, and some authentication tables.

    After I figured out how to browse the localdb database that was created for this project, I decided to try modifying a model object and attempting a Code First Migration with ASP.NET MVC Web Site template sample application. It uses EF to provide login persistence to a localdb. I tried using the

    The demo app already includes a Migrations folder. But if you type add-migration SomeNameHere into the Package manager console, or enable-migrations it seems that it is not possible to work with the ER Migrations with the sample projects.

    I added a string property to the IdentityModel.cs unit, and I tried to manually add it by hand to the 0000...IdentitySchema.cs file, but obviously I don't know how to do that correctly, as when I run the application, I got some errors, shown below. I believe I need to basically have the Entity Framework code-first tool generate some skeleton .cs unit that's going to go into the Migrations folder.

    The usual things people suggest to do now are:

    • Make sure you ran as administator. (Done)
    • Make sure you uninstall the Entity Framework and Reinstall it into the Solution you want it active in(Done)

    Reinstallation looked like this from package manager console:

    PM> Uninstall-Package EntityFramework
    Uninstalling NuGet package EntityFramework.7.0.0-beta4.
    Successfully uninstalled 'EntityFramework.7.0.0-beta4' from WebApplicationDNX.
    PM> Install-Package EntityFramework -IncludePrerelease
    Installing NuGet package EntityFramework.7.0.0-beta4.
    Successfully installed 'EntityFramework.7.0.0-beta4' to WebApplicationDNX.
    PM> 
    

    Here I believe I'm making a mistake because I'm trying to run a command like add-migration and I'm unable to do it. It turns out this was never supported for me to try stuff like this:

    PM> add-migration DummyMigrate
    add-migration : The term 'add-migration' is not recognized as the name of a cmdlet, function, script file, or operable program. .... 
    PM> enable-migration
    enable-migration : The term 'enable-migration' is not recognized as the name of a cmdlet, function, script file, or operable program. 
    ...
    

    Is it possible to get an entity framework migration to work from Visual Studio 2015, from an ASP.NET 5 Preview Templates -> Web Site template based application?

    (updates 1 to 4 removed as they are for useless historical betas)

    Update 5: In asp.net core 1.0.0-RC2 the dnx tool has gone away, replaced by dotnet, but the underlying principle that the dotnet ef command must be run from your project source directory not from your solution directory, and to do so, you should probably use an external command prompt or use PowerShell Tools for Visual Studio, not the nuget command line prompt.

       solution-dir           project-source-dir
             |                       |
    d:\dev\AspNetCoreWebAp\src\AspNetCoreWebAp>dotnet ef
    Project AspNetCoreWebAp (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
    Compiling AspNetCoreWebAp for .NETCoreApp,Version=v1.0
    Compilation succeeded.
        0 Warning(s)
        0 Error(s)
    Time elapsed 00:00:03.0846647
    
                         _/\__
                   ---==/    \\
             ___  ___   |.    \|\
            | __|| __|  |  )   \\\
            | _| | _|   \_/ |  //|\\
            |___||_|       /   \\\/\\
    Entity Framework .NET Core CLI Commands 1.0.0-preview1-20901
    Usage: dotnet ef [options] [command]
    

    enter image description here

  • Warren  P
    Warren P almost 9 years
    Works now in Visual Studio 2015 release.
  • Don
    Don over 8 years
    @Claies, no working for me under Visual Studio 2015 community version. My EntityFramework version is 6.1.3 and EntityFramework.Commands version is (7.0.0-rc1-final).
  • Warren  P
    Warren P about 8 years
    This answer no longer works in EF7, in Visual Studio 2015, update 1. I have to go to the command prompt and run dnx ef migration.... commands from a command prompt
  • Kalpesh Satasiya
    Kalpesh Satasiya about 8 years
    Thanks. I have followed same steps and i am able run the DNX command now.
  • Krishna Veeramachaneni
    Krishna Veeramachaneni about 8 years
    Like Warren P said, this no longer works with EF7 in VS 2015 update 1. I used the dnx commands and followed this article bricelam.net/2014/09/14/migrations-on-k.html
  • Rahatur
    Rahatur about 8 years
    Worked for me as well!
  • c0d3p03t
    c0d3p03t about 8 years
    Totally fixes this issue, I suspect with subsequent releases all these hoops we are having to jump thru will be lessened but in the meantime follow the instructions in this post exactly and you will be able to create your migration :) Thanks!
  • Craig Bruce
    Craig Bruce about 8 years
    This needs to be the accepted answer. Worked for me, thanks
  • Warren  P
    Warren P almost 8 years
    Don't use the package manager console as a way to run EF migration commands.
  • Justin T Conroy
    Justin T Conroy almost 8 years
    @WarrenP, so what should we use now to run EF migration commands? Is opening up a prompt separate from visual studio the only way now?
  • Warren  P
    Warren P almost 8 years
    No because you should use dotnet now in rc2 not dnx
  • Warren  P
    Warren P almost 8 years
    Theres a nice powershell tool for vs that also works if you want to do it in vs
  • Dr. Hilarius
    Dr. Hilarius over 7 years
    This didn't work for me. Creating a new web app didn't recreate the .dnx folder. I restored the .dnx folder and ran the upgrade, which added the bins to the PATH variables, but the command is still not available anywhere but in the .dnx folder itself.