2019年12月9日 星期一

.Net Core 問題4 DB First

必須安裝下列安裝(套件管理主控台)
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.0
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design -Version 1.1.6
Install-Package Microsoft.EntityFrameworkCore.Tools -Version 3.1.0
//第一次乾淨時
Scaffold-DbContext "Server=192.168.0.1;Database=Sample;user id=sa;password=1234567;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Project sample.model
//有更新時
Scaffold-DbContext "Server=192.168.0.1;Database=Sample;user id=sa;password=1234567;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Project sample.model -Force
//需與原資料庫同檔名時
Scaffold-DbContext "Server=192.168.0.1;Database=Sample;user id=sa;password=1234567;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Project sample.model -use-database-names

常遇到Build failed或是直接沒反應
先將其他專案移除將sample.model的專案設為起始專案
在執行在加回其他專案
//第一次乾淨時
Scaffold-DbContext "Server=192.168.1.1;Database=sample;user id=sa;password=xxx;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Project sample.model -UseDatabaseNames
//有更新時
Scaffold-DbContext "Server=192.168.1.1;Database=sample;user id=sa;password=xxx;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models  -Project sample.model -Force -UseDatabaseNames
 
  
參考來源
ASP.NET Core 3.0 如何使用 Database First
“Build failed” on Database First Scaffold-DbContext