MTplus
MTplus

Reputation: 2391

How can I use DB first approach in .NET Core 6?

I try to use Entity Framework in a minimal API running in a .NET Core 6 project. As far as I understand I should run dotnet ef dbcontext scaffold xxxx which I do, I don't get any errors but no models are added to my project.

Can someone please explain how I can use DB first approach with CRUD functionality?

Upvotes: 1

Views: 8258

Answers (4)

Abooraja Rajabpour
Abooraja Rajabpour

Reputation: 417

This sample command should work

dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=ShopDB" Microsoft.EntityFrameworkCore.SqlServer --output-dir Models

Upvotes: 2

SubXero
SubXero

Reputation: 11

As mentioned in the comments, I'm using EF Core Power Tools, too. Sometimes, EF Core Power Tools gives errors about failure and you can see what you are missing. Definitely should be given a chance...

At the MS EF learn website, they said that "Generates code for a DbContext and entity types for a database. In order for this command to generate an entity type, the database table must have a primary key". Also there are a lot of informations.

Rarely, we can forget to add keys to our tables. Tables' fields' types maybe incompatible.

EF Core | MS Learn

Upvotes: 0

Michael Kassa
Michael Kassa

Reputation: 151

I just went through that exercise, and followed the following steps.

First Using nuget package manager install the following: Microsoft.EntityFrameworkCore.Design and Microsoft.EntityFrameworkCore.SqlServer

Then run the following in package manager console:

dotnet ef dbcontext scaffold "Data Source=.;Initial Catalog=databaseName;Trusted_Connection=true" Microsoft.EntityFrameworkCore.SqlServer --output-dir Entities

I did get the error below

The Entity Framework tools version '5.0.14' is older than that of the runtime '6.0.8'. Update the tools for the latest features and bug fixes. See https://aka.ms/AAc1fbw 

Then I run the following script in package manager console:

dotnet tool update --global dotnet-ef --version 6.0.8

Upvotes: 0

Dutt93
Dutt93

Reputation: 126

You should use Frameworks as Microsoft.NETCore.App

Packages from NuGet Package Manager

  1. Microsoft.EntityFrameworkCore
  2. Microsoft.EntityFrameworkCore.SqlServer

Now you can run the command:

Scaffold-DbContext "Server=10.10.10.123;Database=Testdb; user id=sa; password=Sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context TestDBContext -Tables employee

Upvotes: 0

Related Questions