简体   繁体   中英

“the term 'add-migration' is not recognized as the name of a cmdlet” visual studio 2019

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
在此处输入图像描述

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

  1. Tools
  2. NuGet Package Manager
  3. Manage NuGet Package for Solution
  4. Install Microsoft.EntityFrameworkCore.Tools
  5. Try again

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

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://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM