DATABASE
CONNECT TO .NET
Interdisciplinary project
2HM
Ha Noi, Viet Nam Email: Thehieu0814@gmail.com
1
IN FILES appsettings.json
In file appsettings.json
"ConnectionStrings": {
"DefaultConnection":
"Server=MEW\\SQLEXPRESS;Database=2HM_Shop;Trusted_Connection=True;Multi
pleActiveResultSets=true;Encrypt=False;"}
Let’s go
• CD solution and Install package EntityFramework:
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Note:
Check !EntityFramework
dotnet tool install --global dotnet-ef
dotnet ef –version
• CD project and Install package EntityFramework.Design:
dotnet add package Microsoft.EntityFrameworkCore.Design
Note:
Set as startup project
• In folder Models
+ Create file ApplicationDbContext.cs:
Is where you define the DbContext class. This is one of the most important
files in defining and interacting with the database.
Is the key place to define and manage the entire database interaction process
in an ASP.NET Core project using Entity Framework Core.
- Connect to Database:
- Table Definitions and Relationships:
- Determine the Connection String:
- Run Migration Commands:
- Initial Data Initialization (Seed Data):
- Execute LINQ Queries:
- Ensuring Data Consistency:
+ Create class (table in Database): eg: Product.cs
• In file HomeController.cs
• In file Program.cs ( run project )
• End:
Return terminal and run:
dotnet ef migrations add InitialCreate
dotnet ef database update
(Use: Create database in SQL server )
HAPPY ENDING : )