---
# System prepended metadata

title: 20200210 .NET Core and Template Workshop

---

# 20200210 .NET Core and Template Workshop

## 預先要求
1. 安裝 .NET Core SDK 3.1.xxx or above
   - https://dotnet.microsoft.com/download
2. IDE，安裝以下所列出之IDE選項，三選一，並更新到最新版
   - Visual Studio Code
   - Visual Studio 2019
3. 資料庫，使用 localdb 或是使用 docker 之資料庫
  - Docker
    1. 安裝 Docker
    2. 執行以下指令，因為此指令需要龐大網路資源，所以最好於 WORKSHOP 前執行完畢
    ``` console
       docker pull mcr.microsoft.com/mssql/server:2019-latest-ubuntu
    ```
    https://docs.microsoft.com/zh-tw/sql/linux/quickstart-install-connect-docker?view=sql-server-linux-ver15&pivots=cs1-cmd
  - localdb
    - 如果有安裝 Visual Studio 就已經有 localdb
    - Visual Studio Code 沒有 localdb
4. 資料庫管理介面，以下二選一
  - Azrue Data Studio
    - https://docs.microsoft.com/zh-tw/sql/azure-data-studio/download
  - SSMS
    - https://docs.microsoft.com/zh-tw/sql/ssms/download-sql-server-management-studio-ssms

## 範例資料庫

啟動 Dokcer 之 SQL Server

``` ps1
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=P@ssw0rd" -p 1433:1433 --name mssql2017 -d mcr.microsoft.com/mssql/server:20197-latest

```

- SampleDb
   - https://gist.github.com/lettucebo/87c5f12c2493f05f3db632620566cbae

使用資料庫管理介面執行上述 SQL 檔案

OR

``` ps1
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=P@ssw0rd" -p 1433:1433 --name mssql2017 -d abc12207/sample-db:latest
```

## 安裝 Template

>這邊的安裝是指將自訂的範本加到可使用的範本列表裡

查看可使用的範本:
``` console
> dotnet new --list  
```

### 安裝公司範本

Nuget package download: [Ci.Base.Template.Mvc.nupkg](https://dev.azure.com/creatidea/2019.Ci.Base/_packaging?_a=package&feed=Ci.Nuget&package=Ci.Base.Template.Mvc&protocolType=NuGet)

使用 dotnet CLI 來安裝前，必須先執行以下腳本來安裝 [`Azure Artifacts Credential Provider`](https://github.com/Microsoft/artifacts-credprovider)
``` powershell
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1'))
```

then use dotnet CLI to install template.
``` console
> dotnet new --install Ci.Base.Template.Mvc --nuget-source https://pkgs.dev.azure.com/creatidea/_packaging/Ci.Nuget/nuget/v3/index.json
```

或是使用下載方式來安裝
then use dotnet CLI to install template.

``` console
> dotnet new --install "{nupkg path}"
```

Example: 
``` console
> dotnet new --install "C:\Users\Money\Downloads\ci.base.template.mvc.3.1.0.nupkg"
```



> 因為 Ci.Nuget 需要登入才能使用，而 dotnet new 目前不支援 --interactive，所以無法直接以連結安裝，待未來支援後即可直接透過連結安裝
 
## 新增方案

Use dotnet CLI to create solution.

``` console
dotnet new cimvc --name="ProductTest"
```

After create the solution, it's done, there is no need to configure before start develop the application.

However you still need to add the database and logger storage by yourself.

## 基本設定

https://docs.microsoft.com/zh-tw/ef/core/miscellaneous/cli/dotnet

### 安裝 EF Core

```
dotnet tool install --global dotnet-ef
```

### 匯入資料庫
Use Sample database.

Use `Package Manager Console` (套件管理器主控台)
``` ps1
Scaffold-DbContext "Data Source=127.0.0.1;Initial Catalog=Sample;Persist Security Info=True;User Id=sa;Password=P@ssw0rd;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir "Model/Sample" -Project "Lab.Core" -StartupProject "Lab.Api"
```

Use `dotnet CLI`
``` ps1
dotnet ef dbcontext scaffold "Data Source=127.0.0.1;Initial Catalog=Sample;Persist Security Info=True;User Id=sa;Password=P@ssw0rd;" Microsoft.EntityFrameworkCore.SqlServer --output-dir "Model/Sample" --project "Lab.Core" --startup-project "Lab.Api" --force
```

| 參數 | prefix | 範例 |
| -------- | -------- | -------- |
| 連線字串  |      | "Data Source=.;Initial..."     |
| Nuget 套件名| | Microsoft.EntityFrameworkCore.SqlServer|
| 產生檔案的目的專案| -\-project(-p)| Lab.Core |
| 程式啟始專案| -\-startup-project(-s)| Lab.Api |
| 目的專案中的資料夾| -\-output-dir(-o)| Model/Sample |
| 自訂檔案名稱(預設資料庫名)| -\-context(-c) | Sample |
| 覆蓋現有檔案| -\-force(-f)||

> 記得要修改要放在哪個資料夾裡


### 修改預設設定 (**每次匯入都要做一次**)
Remove database connection string from `DbContext`

make sure `OnConfiguring` method is empty (Remove hard code connection string)
``` cs
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
```

Add global query filter for IsDelete flag (For defualt IsDelete filter)
``` cs
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    GlobalQueryFilter.SetFilter(modelBuilder);

    // ...
}
```

After add the DbContext, you need to use `AddDbContext` at `Starup.cs` `ConfigureServices`

``` cs
services.AddDbContext<SampleContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:SampleConnStr"]));
```

Add the connection string to the `appsettings.json`

``` json
"ConnectionStrings": {
    "SampleConnStr": "Data Source=127.0.0.1;Initial Catalog=Sample;Persist Security Info=True;User Id=sa;Password=P@ssw0rd;",
}
```

## Sample Code
Use products as sample

### List

### SetDelete

### Delete