Saturday, February 15, 2025
HomeUncategorizedCách tạo model từ một Table có sẵn trên DB trong EF...

Cách tạo model từ một Table có sẵn trên DB trong EF Core

Vào appsetings.json để tìm tên của database connection string

Thực hiện chạy lệnh trong Package manager console (PMC)

Scaffold-DbContext "Name=PathManagementConnectionString" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables dbo.tbSYS_Users -Context PathManagementDbContext

Trong đó theo sau -OutputDir là thư mục sẽ chứa code tạo ra

Theo sau -Tables là tên bảng trong SQL Server

Theo sau -Context là tên của DbContext sẽ tạo ra mới

Kết quả sau khi chạy lệnh thành công trên PMC

Hai file mới sẽ tạo ra đó là file model và file dbContext

Chúng ta có thể thấy ở hàm OnModelCreating sẽ tạo ra bộ quy tắt cho các thuộc tính của model

Chúng ta có thể dùng các attribute để thay thế

Nguyễn Minh Châu
Nguyễn Minh Châuhttps://nhatkydev.com
Hi guys ! I'm a software developer. I love programming and new technologies. I create non-professional content on this website, you can only view it for reference purposes.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular