0% found this document useful (0 votes)
12 views

Database Dotnet

The document provides instructions for connecting a .NET project to a SQL Server database using Entity Framework Core, including installing necessary packages, defining the database context and models, and running migrations to initialize the database structure. Developers are directed to create a ApplicationDbContext file to define the database interaction and tables like Products, and shown how to execute migrations to create the database. Following these steps allows a .NET project to connect to and interact with a SQL database using Entity Framework Core.

Uploaded by

Lợi Đào
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Database Dotnet

The document provides instructions for connecting a .NET project to a SQL Server database using Entity Framework Core, including installing necessary packages, defining the database context and models, and running migrations to initialize the database structure. Developers are directed to create a ApplicationDbContext file to define the database interaction and tables like Products, and shown how to execute migrations to create the database. Following these steps allows a .NET project to connect to and interact with a SQL database using Entity Framework Core.

Uploaded by

Lợi Đào
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DATABASE

CONNECT TO .NET
Interdisciplinary project

2HM

Ha Noi, Viet Nam Email: [email protected]

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 : )

You might also like