step3.需要把上下文對象注入IOC容器中(Startup.cs)
AddContext 裡面要放資料庫連接字串,因此將連接字串放入appsettiong.json中方便調用
創建倉庫
創建數據庫前(這個例子是先創造數據模型,再創造DB的Table)
因此數據模型的限制要先設定好,例如主鍵值,必填之類的
安裝數據庫創建工具(數據模型自動產生DB table)
使用dotnet命令列
cd 到 資料夾
dotnet ef migrations add initialMigration
//創建Migrations文件夾,文件夾中有日期的C#程式,這個文件就是數據遷移代碼dotnet ef database update
//執行資料庫更新windows 使用會有問題,因為自從.net 3.0開始,EF 命令工具就被移除,因此要先加回來。全局安裝ef工具:dotnet tool install --global dotnet-ef