Generate a model based on the database (sever first method) (understand at a glance)

Ready to work

1 First open the database, and there is a well-established database in the database, there are tables in the database, and there must be content in the tables.

2 Open VS 2019. Take a new project as an example.

I am here to take the Test2 project as an example.

If there is a project, test it from the existing project. If there is no new project, the new project must install the Nuget package.

There are two ways to install Nuget.

One is the tool-package manager console-the way to enter commands

For example, the following command

Install-package Microsoft.EntityFrameworkCore

Install-package Microsoft.EntityFrameworkCore.SqlServer

Install-package Microsoft.EntityFrameworkCore.Tools

One is to search for the package name.




Note: In a project, 3 packages have been installed, and the following test files do not need to install packages. If you create a new project, you must reinstall the package.

Important: After the above is completed.

Scaffold-DbContext "Server=.;database=test1(需要的数据库名字);Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models2

What I implemented myself is:

Scaffold-DbContext "Server=.;database=Test03;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models2

According to the model generated by the database Test03, the generated Test03 code model.

The code inside is required to be understood. You have work to do again.

Reference URL