Create an ASP.NET Core MVC application with database-first approach

178 views Asked by At

I want to create an ASP.NET Core MVC application. It seemed it was no good idea to use a .NET Framework project, since the abandon of Entity Framework 6.

So, I try .NET Core, and I read this: Entity Framework Core (EF Core) Database First Approach - Dot Net Tutorials

I installed Microsoft.EntityFrameworkCore.Design, and now I type

Scaffold-DbContext "Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;Encrypt=True;Trust Server Certificate=True"

In the Nuget Package console, I am answered that Scaffold-DbContext is not a recognized applet command name, script, command ...

If, just in case, I try the same in the "Immediate" window, I am answered that Data is not a valid format specifier (CS0726).

In the "Add, New Element" contextual menu entry, I have no "Entity Data Model Wizard".

Do you see what I missed ?

I'm using

  • Visual Studio 2022 Community
  • Windows 11
  • SQL Server 2014 Express
3

There are 3 answers

3
Qiang Fu On

For Asp.net-core project to use Scaffold-DbContext You need to install following packages:

Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Tools

And the ASP.NET Core doesn't support "ADO.Net Entity Data Model Wizard". It can only be used in old .NET framework project.

I suggest you try this sample https://medium.com/@certosinolab/exploring-database-first-approach-with-entity-framework-in-net-core-6-db86a822d72e

4
Fredou On

Since .net core doesnt easily support scaffolding, someone create a extension for visual studio caller EF Core Power Tools, I will suggest that you take a look

It can reverse engineer the selected database, creating the dbcontext and model with custom option

I have used it in many, many applications and so far it work nicely

0
user3500176 On

Yesterday, I eventually displayed the data from the table, although the connection string was stored in a not really recommended place.

Mainly I used elements given in this thread, and also the search result about "service not found" (in substance).

After that, my machine crashed, I had to reinstall many things, and the new Program.cs contains less elements than I remember from yesterday, and the context is not found to run the command I just mentioned.

So, I shall come back, maybe in a week or so, to provide a more complete answer.

It is not impossible I have to ask for guidance in the interval, but with no doubt it will be much clearer in a different thread.