###### tags: `DotNet Core` `Entity Framework` # EF Core DB First測試 近期要寫簡單的Web API,在使用已包好的DB類別庫剛好沒有設計自己要得功能。因為基本只需做簡易的Query API,於是嘗試自己重新建立DB Context。 目前已有現成的DB類別庫,就不以Code First角度出發去建置DBContext。想說能不能透過EF Cli快速建置Model與Context這件事情。稍微找了一下確實有相關解法 - 1. 安裝EntityFrameworkCore資料庫Database Providers。透過稱為Provider外掛程式庫可存取各種不同的資料庫([Provider Item](https://docs.microsoft.com/zh-tw/ef/core/providers/?tabs=dotnet-core-cli))。這邊我選用Microsoft.EntityFrameworkCore.SqlServer - 2. 安裝Microsoft.entityframeworkcore.design套件。 稍微列一下安裝項目如下 ![](https://i.imgur.com/fDej3YY.png) - 3. 接著下Command如下 ```scrip= dotnet ef dbcontext scaffold "server=xxxxxx;Database=xxxxxx; User=xxxxxx;Password=xxxxx;" "Microsoft.EntityFrameworkCore.SqlServer" -o ./Models -c XXXXContext -f ``` 需要設置幾個參數 - DB Server Source - Database Name - User Name - User Password - Provider Package Name - o:Output路徑 - c:Context - f:複寫 設置完相對應參數下完指令後,即可產生相對應資料庫Table Entity與Context。記得Connection Sting要替換成從檔案讀取! ![](https://i.imgur.com/qPC5BEt.png)