"the term 'add-migration' is not recognized as the name of a cmdlet" visual studio 2019
Asked Answered
C

4

8

enter image description here

I'm trying to use entity frame work core and have a fresh asp.net core project open. When i attempt to install Microsoft.EntityFrameworkCore.SqlServer through nuget package manager i get the following error.

Package Microsoft.EntityFrameworkCore.SqlServer 3.0.0 is not compatible with net461 (.NETFramework,Version=v4.6.1). Package Microsoft.EntityFrameworkCore.SqlServer 3.0.0 supports: netstandard2.1 (.NETStandard,Version=v2.1)

When i click this i get sent to .csproj file.

In that file i changed the target framework from <TargetFramework>net461</TargetFramework> to <TargetFramework>netstandard2.1 </TargetFramework> as the error implies.

Doing this gets rid of the error but when i use the command 'add-migration' in package manager console i get this error.

"The term 'add-migration' is not recognized as the name of a cmdlet"

I'm not sure what to do going forward. Please help. I hope i've explained the issue clearly.

Deleting the project.assets file or updating nuget package manger and using the the command "enable-migrations" hasnt worked either.

enter image description here

Cahra answered 4/10, 2019 at 9:51 Comment(8)
please make sure you are using latest VS2019. Default Target framework for .net 3.0 should be netcoreapp3.0. I think you have selected wrong template. Check: learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/… - you have to install dotnet ef because it is not a part of SDK anymoreUrbano
i tried to embed an image into the post to illustrate what i did. I chose .Net core and ASP.NET CORE 3.0 in the template section and i was able to install Microsoft.EntityFrameworkCore.SqlServer with ease. Thanks Konrad! much appreciated.Cahra
the command to use now is dotnet ef migrations. However, when i do this and give the migration a name, its says " No project was found. Change the current working directory or use the --project option." even though its pointing to my project in the drop down.Cahra
open command line, change directory to your csproj file, then run dotnet ef command.Urbano
Thanks a bunch! it worked! I had a few more hiccups after your solution. I'll share the links i used below to solve the problem for others.Cahra
learn.microsoft.com/en-gb/ef/core/miscellaneous/cli/…Cahra
entityframework.net/knowledge-base/52674430/…Cahra
It helped to install Microsoft.EntityFrameworkCore.ToolsPostwar
S
18

The following helped me to add the missing tools to .net core 3.1

  1. Install-Package Microsoft.EntityFrameworkCore.Tools

  2. Update-Package Microsoft.EntityFrameworkCore.Tools

  3. Get-Help about_EntityFrameworkCore

From this Blog

Stratum answered 21/3, 2020 at 21:14 Comment(2)
You may also need to target a specific version as such: install-package Microsoft.EntityFrameworkCore.Tools -version 3.1.10Moia
I am using Microsoft.EntityFrameworkCore ver 7.0.0. I ran "Install-Package Microsoft.EntityFrameworkCore.Tools" and problem solved.Hudson
K
5
  1. Tools
  2. NuGet Package Manager
  3. Manage NuGet Package for Solution
  4. Install Microsoft.EntityFrameworkCore.Tools
  5. Try again
Kelly answered 13/8, 2020 at 20:39 Comment(0)
V
1

I have faced the same issue and these steps fixed it..

1) Tools -> Nuget Package Manger -> Package Manager Settings

2) General Tab

3) Clear All NuGet Cache(s)

4) Restart Visual Studio

Ventriloquy answered 5/10, 2019 at 12:29 Comment(0)
P
1

When adding your first migration to a new package:

  1. Create the data model class (with the columns and data types for your table(s); see the Source link below for details)
  2. Add a New Folder in the Pages directory (recommendation: use the name of your primary/parent DB Table - that is, the table that doesn't have any foreign keys)
  3. Right-click on that new folder > Add > New Scaffolded Item > "Razor pages using Entity Framework (CRUD)" > Add
  4. Select your primary model class
  5. Click "+" to add a data context class (this is the big critical ingredient, which is automated here)
  6. Change the Data context class name by replacing "Models" with "Data" (so it will create the database context class with the correct namespace)
  7. Click Add

IF you created more than one model (to create multiple tables in your database), follow these next steps also:

  1. Open the file: Data<YourNamespace>Context.cs
  2. Duplicate line 17 for each of your models (DB tables)

In all cases proceed with the 2 following commands in Package Manager Console (Tools [from menu at the top] > NuGet Package Manager > Package Manager Console):

  1. Add-Migration <NameYourMigration>, which will generate the migration code (creates a new class in the Migrations directory, which new class's file name begins with a timestamp)
  2. Update-Database, which will run the use the Up() method from the new class file it just created in the Migrations directory BAM! You should now have (a) new database table(s) in your database!

To see the database tables:

  1. View (from menu at the top) > SQL Server Object Explorer > SQL Server > (localdb)\MSSQLLocalDB... > Databases > Context... > Tables
  2. To see the data definition: double-click on the table name
  3. To see the data (empty tables right now): right-click on the table name > View Data

Source (with images, but some steps are a spread out in the tutorial): https://learn.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model

Pinhead answered 3/11, 2020 at 21:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.