4
votes

enter image description here
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
enter image description here

4
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: docs.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 anymoreKonrad
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.Ahmed Bashir
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.Ahmed Bashir
open command line, change directory to your csproj file, then run dotnet ef command.Konrad
It helped to install Microsoft.EntityFrameworkCore.Toolsvidriduch

4 Answers

15
votes

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

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

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

1
votes

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