Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't Enable-Migrations or Add-Migration : The term 'xxx' is not recognized as the name of a cmdlet #1196

Closed
jbuiss0n opened this issue Dec 2, 2014 · 22 comments
Assignees
Milestone

Comments

@jbuiss0n
Copy link

jbuiss0n commented Dec 2, 2014

Hello,

I don't know if i'm in the right place to ask my question, sorry if not.

I want to try some little things using EF Code migrations, so i've created a new ASP.NET 5 Console Application, using Visual Studio 2015 Preview.
I then just go to my Package Manager Console, and put Install-Package EntityFramework -Pre. Everything is ok, i've the right dependencies in my project.json.

I write some code, adding a BlogContext with 2 little entities (Blog / Post), everything looks fine, as i just copy/paste the sample here https://github.com/aspnet/EntityFramework/wiki/Using-EF7-in-Traditional-.NET-Applications.

Finally, i want to enable migrations, so i'm back in my Package Manager Console, and put Enable-Migrations but this error happens : The term 'Enable-Migrations' is not recognized as the name of a cmdlet.

As this command is not indicated in the EF wiki, i proceed to directly execute the command Add-Migration MyFirstMigration, but same error: The term 'Add-Migration' is not recognized as the name of a cmdlet.

I've tried to reboot VS, to reboot Windows, to re-install Nuget, to reset the Nightly Builds packages source, but nothing went better...

So, i tried the exact same things using VS 2013 Update 4 and a classic Console Application, and it works fine !

But i really want to use ASP.NET 5, and the K projets style... Do someone have any idea about my problem ? Did i miss something ?

Thanks,
Jeremy

@jbuiss0n jbuiss0n changed the title [Question] Can't Enable-Migrations or Add-Migration : The term 'xxx' is not recognized as the name of a cmdlet Can't Enable-Migrations or Add-Migration : The term 'xxx' is not recognized as the name of a cmdlet Dec 2, 2014
@bricelam
Copy link
Contributor

bricelam commented Dec 2, 2014

To get started, check out my post on Entity Framework 7 Migrations in ASP.NET vNext

Also, #1049 and #978 should make the experience better.

@jbuiss0n
Copy link
Author

jbuiss0n commented Dec 3, 2014

Thanks for your help ! I still have an error, but it's evolving !

  • I've first make an update using kvm upgrade, I was not up to date, but it's ok now. I restart the console and VS (to be sure it load the right bin).
  • Then, I go to my Project's root and execute kvm restore, everything looks good.
  • Now, I execute in my src\MyProject the command k ef, and it's cool, i've the unicorn! :)
  • Finnaly, I run k ef migration add InitialSchema, and the pain beggins...
Using context 'ApplicationDbContext'.
System.Exception: TODO: No service for type 'Microsoft.Data.Entity.Infrastructure.DbContextService`1[Microsoft.Data.Entity.Migrations.Infrastructure.Migrator]' has been registered.
   à Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   à Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
   à Microsoft.Data.Entity.Commands.MigrationTool.CreateMigrator(DbContext context)
   à Microsoft.Data.Entity.Commands.MigrationTool.AddMigration(String migrationName, String rootNamespace, String contextTypeName)
   à Microsoft.Data.Entity.Commands.Program.AddMigration(String name, String context)
   à Microsoft.Data.Entity.Commands.Program.<>c__DisplayClass4.<Main>b__16()
   à Microsoft.Framework.Runtime.Common.CommandLine.CommandLineApplication.Execute(String[] args)
   à Microsoft.Data.Entity.Commands.Program.Main(String[] args)

I'm sure i miss something, but after long search everywhere, I don't find what...

Just in case, here is my project.json

{
  "version": "1.0.0-*",
  "dependencies": {
    "EntityFramework": "7.0.0-*",
    "EntityFramework.SqlServer": "7.0.0-*",
    "EntityFramework.Commands": "7.0.0-*"
  },
  "commands": {
    "run": "run",
    "ef": "EntityFramework.Commands"
  },
  "frameworks": {
    "aspnet50": { },
    "aspnetcore50": {
      "dependencies": {
        "System.Console": "4.0.0-*"
      }
    }
  }
}

@bricelam
Copy link
Contributor

bricelam commented Dec 3, 2014

Now you're seeing a bug that was fixed yesterday by #1182. Try again with the latest nightly.

@rowanmiller rowanmiller added this to the Discussions milestone Dec 3, 2014
@jbuiss0n
Copy link
Author

jbuiss0n commented Dec 4, 2014

Thanks, but it looks like i'm cursed...
I've now this error message:

'EntityFramework.Commands' does not contain a static 'Main' method suitable for an entry point

@jbuiss0n
Copy link
Author

jbuiss0n commented Dec 9, 2014

Ok so after searching everything and everywhere, i think i've find my issue...

It seems to be a problem (or maybe i'm the problem...) with kvm. When i run kvm upgrade it install new KRE versions, and the command kvm list shows me:

Active Version            Runtime Architecture Location                      Alias
------ -------            ------- ------------ --------                      -----
  *    1.0.0-alpha3       svr50   x86          C:\Users\Jérémy\.kre\packages default
       1.0.0-alpha4-10353 svr50   x64          C:\Users\Jérémy\.kre\packages
       1.0.0-alpha4-10353 svr50   x86          C:\Users\Jérémy\.kre\packages
       1.0.0-alpha4-10353 svrc50  x64          C:\Users\Jérémy\.kre\packages
       1.0.0-alpha4-10353 svrc50  x86          C:\Users\Jérémy\.kre\packages
       1.0.0-beta1        CLR     amd64        C:\Users\Jérémy\.kre\packages
       1.0.0-beta1        CLR     x86          C:\Users\Jérémy\.kre\packages
       1.0.0-beta1        CoreCLR amd64        C:\Users\Jérémy\.kre\packages
       1.0.0-beta1        CoreCLR x86          C:\Users\Jérémy\.kre\packages
       1.0.0-beta2-10760  CLR     x86          C:\Users\Jérémy\.kre\packages

What I understand is that I have to target 1.0.0-alpha4-10353 svr50, so I try to change my active version, but don't find how... the command kvm use 1.0.0-alpha4-10353 seems to work, but when I restart my console (because the PATH changed...), i'm still with aplha3.

I've finally changed manually my PATH to use KRE-svr50-x64.1.0.0-alpha4-10353, and everything's work nice now.

Hope it can help someone.

@bricelam
Copy link
Contributor

bricelam commented Dec 9, 2014

Closing since everything seems to be working now.

@bricelam bricelam closed this as completed Dec 9, 2014
@egorpavlikhin
Copy link

@bricelam your blog doesn't work.
And I'm still getting this error in 7.0.0-beta3-11980

G:\!Projects\Hostilling\src\Hostilling>k ef migration add 0001
Using context 'ApplicationDbContext'.
System.InvalidOperationException: No service for type 'Microsoft.Data.Entity.Infrastructure.DbContextService`1[Microsoft.Data.Entity.Relational.Migrations.Infrastructure.Migrator]' has been registered.
at M icrosoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.Data.Entity.Commands.MigrationTool.CreateMigrator(DbContext context)
at Microsoft.Data.Entity.Commands.MigrationTool.CreateScaffolder(DbContext context)
at Microsoft.Data.Entity.Commands.MigrationTool.AddMigration(String migrationName, String contextTypeName, String rootNamespace, String projectDir)
at Microsoft.Data.Entity.Commands.Program.AddMigration(String name, String context)
at Microsoft.Data.Entity.Commands.Program.<>c__DisplayClass5_1.<Main>b__6()
at Microsoft.Framework.Runtime.Common.CommandLine.CommandLineApplication.Execute(String[] args)
at Microsoft.Data.Entity.Commands.Program.Main(String[] args)

@egorpavlikhin
Copy link

Figured it out:
This code must be in Startup.cs public void ConfigureServices(IServiceCollection services) method, not anywhere else, otherwise it won't work.

services
    .AddEntityFramework(Configuration)
    .AddSqlServer();

@rowanmiller
Copy link
Contributor

@egorpavlikhin - correct, we match that method by convention. We do have an item to make all this more robust by RTM.

@julielerman
Copy link

Hey I just want to clarify something here. I'm NOT using aspnet5 but instead a basic full .NET app ..console (and yes, I understand that it's not recommended, I'm just using that to explore) Instead of having a paramterless ctor on dbcontext and specifying UseSqlServer with connection string in onconfiguration, I am passing in the dbcontextoptions. So in this scenario, if I am not using d.i., I won't succeed with migrations, right? Therefore if I just want to show how you could continue to use migrations in a familiar way, I either have to use DI (on my own since not using aspnet5 or mvc etc) or I have to just go back to the "not grown up" pattern of usesqlserver with connection string in onconfiguring, right?

@AngelVenchev
Copy link

Hi, I still get this error with the latest version.

D:\Programs\VS2015-Projects\MovieAngularJSApp\src\MovieAngularJSApp>k ef migration add initial
Using context 'MoviesAppContext'.
System.InvalidOperationException: No service for type 'Microsoft.Data.Entity.Infrastructure.DbContextService`1[Microsoft.Data.Entity.Relational.Migrations.Infrastructure.Migrator]' has been registered.
    at Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
    at Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
    at Microsoft.Data.Entity.Commands.MigrationTool.CreateMigrator(DbContext context)
    at Microsoft.Data.Entity.Commands.MigrationTool.CreateScaffolder(DbContext context)
    at Microsoft.Data.Entity.Commands.MigrationTool.AddMigration(String migrationName, String contextTypeName, String rootNamespace, String projectDir)
    at Microsoft.Data.Entity.Commands.Program.AddMigration(String name, String context)
    at Microsoft.Data.Entity.Commands.Program.<>c__DisplayClass5_1.<Main>b__6()
    at Microsoft.Framework.Runtime.Common.CommandLine.CommandLineApplication.Execute(String[] args)
    at Microsoft.Data.Entity.Commands.Program.Main(String[] args)</code>

I can provide whatever code is need. I have been googling for 3 hours on this and nothing helped.

@julielerman
Copy link

I haven't done it in a few weeks, but have not had any problems with k ef migrations. The fact that it says no migrator registered makes me wonder if you've specified the provider somewhere. If not, here are ways to do that in an aspnet5 app: https://github.com/aspnet/EntityFramework/wiki/Configuring-a-DbContext#dependency-injection-scenarios

@0xdeafcafe
Copy link
Contributor

Did you ever get this fixed @AngelVenchev ? I'm not getting this bug locally, but when I deploy to azure though git I get it.

@AngelVenchev
Copy link

@0xdeafcafe I fixed it when I implemented with another configuration from @julielerman 's link.

I am not really sure what caused though

@Vassi
Copy link

Vassi commented Feb 28, 2015

I'm assuming this currently also does not work if your context is elsewhere than the Web project. I don't like having the context and EF code in my web project so it usually moves into some Service assembly. In EF6 this is kind of similar where you have to make your web project the default startup, but tell the Package Manager console that you're targeting the assembly with the DbContext in it. There doesn't seem to be an equivalent right now for k ef, is that accurate?

It complains theres no context if I run it in the web project folder and won't even recognize the k ef command in the services project folder, despite the Commands assembly being referenced there.

I understand it's a work in progress, I'm just trying to confirm that I'm not missing anything.

POST BREAK EDIT: After poking around I realized I had to add the ef command to project.json myself, as is done for us in the web template. The second step was to temporarily put my connection string as part of the UseSqlServer call on OnConfiguring call which is not ideal but works just fine if I just need to generate a migration. After that, k ef migration add did exactly what I wanted, thanks for all the hard work!

@ardalis
Copy link

ardalis commented Mar 4, 2015

Ran into this same issue as Vassi with ASP.NET 5 and using a separate project for EF. Currently there seems to be no way to refer to the connection string defined in the web project's config file when running migrations from the command line. My latest problem with CTP6 and a standard sample ASP.NET 5 project (after hard-coding the connection string) is that migrations are failing with "ALTER TABLE DROP COLUMN failed because column 'NormalizedEmail-' does not exist in table AspNetUsers. Not sure why that's happening...

UPDATE: The migration k ef migration add foo created included a bunch of things relating to aspnetusers that were wonky/wrong and unrelated to the model class I had added. I removed all of that from the migration, leaving just my CREATE TABLE / DROP TABLE statements for my new entity, and everything worked.

@rowanmiller
Copy link
Contributor

@ardalis - That latest issue is a bug with the scaffolded code. @bricelam is working on a fix... but in the meantime you just need to fix the scaffolded code to make sure it gets the right overload of ForeignKey(...). Here is what the change looks like rowanmiller/UnicornStore@b71cfc1#diff-ead41050d624017563d55a02d52ab1e1R56

@rowanmiller
Copy link
Contributor

@ardalis - Should also mention that we don't pick up Startup.cs from another project yet... so hardcoding connection string in UseSqlServer in OnConfiguring is the correct workaround.

@luisrudge
Copy link

@rowanmiller if I hardcode the connection string, I won't be able to publish to azure and use a different database.

@0xdeafcafe
Copy link
Contributor

I had this issue @luisrudge, what I did was check if Configuration in Startup was null, if it was, initialize a new one, and use that to check for connection strings in config.json or environment variables. This is my OnConfiguring:

protected override void OnConfiguring(DbContextOptions options)
{
    var config = Startup.Configuration
        ?? new Configuration()
            .AddJsonFile("config.json")
            .AddEnvironmentVariables();

    options.UseSqlServer(config.Get("Data:DefaultConnection:ConnectionString"));
}

@luisrudge
Copy link

Awesome @0xdeafcafe Thanks!

@Padmanaabhah
Copy link

Try the following steps:

  1. Remove all Microsoft.EntityFrameworkCore.Tools references from dependencies and tools sections inside a project.json

  2. Close Package Manager Console (PMC) and restart Visual Studio

  3. Add under dependencies section:

"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
}
4) Add under tools section

"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
5) Restart again Visual Studio 2015

  1. Open the PMC and type

Add-Migration $Your_First_Migration_Name$
That's because the PMC recognize the tools when Visual Studio is starting.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests