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

asp.net-core dnx entity-framework powershell visual-studio-2015

Question

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

1
17
6/6/2016 2:44:39 PM

Accepted Answer

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.

4
6/6/2016 2:46:24 PM

Popular Answer

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow