Entity Framework Core(1)
Posted: Wed Jan 08, 2025 4:26 am
This article will introduce how to use Entity Framework Core to do CRUD in a Web application project using Visual Studio.
1. Create an ASP .NET Core Web application project and select .net6.0. Here we type the project name into EntityFrameworkCoreWebTest, or your own name
. 2. Install the following Nuget package
Microsoft.EntityFrameworkCore //EntityFrameworkCore entity
Microsoft.EntityFrameworkCore.SqlServer //Select based on the SQL you are connected to
Microsoft.EntityFrameworkCore.Tools //It is convenient for us to implement reverse engineering through the package administrator console later.
3. Create a database on SQL Server, or use an existing database. This step can be omitted. Below I will use the local MS SQL name as an example for the TestDB database.
4. After creating the database, prepare to proceed. Reverse engineering and let Entity Framework help us generate the Model code
5. Open the package manager console (View (V) -> Other Windows (E) -> Package Manager Console (O)), and put Visual The startup project of Studio and the default project of Package Manager console have bahrain whatsapp phone number been changed to EntityFrameworkCoreWebTest
Entity Framework Core Example Figure 1
6. After adjusting the default project, enter
Scaffold-DbContext 'data source=.;initial catalog=TestDB;Integrated Security = true' Microsoft.EntityFrameworkCore.SqlServer -OutputDir DBModels -Force
※After Scaffold-DbContext What is connected is the SQL connection string, followed by the database type. Because we are using MS SQL, we write Microsoft.EntityFrameworkCore.SqlServer, followed by the parameters. The -OutputDir used here is the output location. , for example, if we fill in "DBModels" here, a new DBModels folder will be added to the project, and the cs file generated by reverse engineering will be placed in the folder; and -Force will overwrite the existing file, which is convenient When we modify the database structure, we directly edit the file. For more instructions, please refer to Microsoft's official instructions.
7. The relevant cs file will then be generated. , where TestDBContext is the DbContext entity that inherits DBContext, and because there is only the User table in my database, only User.cs is generated.
1. Create an ASP .NET Core Web application project and select .net6.0. Here we type the project name into EntityFrameworkCoreWebTest, or your own name
. 2. Install the following Nuget package
Microsoft.EntityFrameworkCore //EntityFrameworkCore entity
Microsoft.EntityFrameworkCore.SqlServer //Select based on the SQL you are connected to
Microsoft.EntityFrameworkCore.Tools //It is convenient for us to implement reverse engineering through the package administrator console later.
3. Create a database on SQL Server, or use an existing database. This step can be omitted. Below I will use the local MS SQL name as an example for the TestDB database.
4. After creating the database, prepare to proceed. Reverse engineering and let Entity Framework help us generate the Model code
5. Open the package manager console (View (V) -> Other Windows (E) -> Package Manager Console (O)), and put Visual The startup project of Studio and the default project of Package Manager console have bahrain whatsapp phone number been changed to EntityFrameworkCoreWebTest
Entity Framework Core Example Figure 1
6. After adjusting the default project, enter
Scaffold-DbContext 'data source=.;initial catalog=TestDB;Integrated Security = true' Microsoft.EntityFrameworkCore.SqlServer -OutputDir DBModels -Force
※After Scaffold-DbContext What is connected is the SQL connection string, followed by the database type. Because we are using MS SQL, we write Microsoft.EntityFrameworkCore.SqlServer, followed by the parameters. The -OutputDir used here is the output location. , for example, if we fill in "DBModels" here, a new DBModels folder will be added to the project, and the cs file generated by reverse engineering will be placed in the folder; and -Force will overwrite the existing file, which is convenient When we modify the database structure, we directly edit the file. For more instructions, please refer to Microsoft's official instructions.
7. The relevant cs file will then be generated. , where TestDBContext is the DbContext entity that inherits DBContext, and because there is only the User table in my database, only User.cs is generated.