Backend/C_shape/README.md

142 lines
5.3 KiB
Markdown
Raw Permalink Normal View History

2024-04-22 12:26:36 +08:00
# .NET
## 介紹
### 目前都以.NET6去做開發有以下特點
* 簡化的開發:非常容易上手。 C# 10中的新語言功能可減少您需要撰寫的程式碼數量。 對 Web 堆疊和最少 API 的投資,可讓您輕鬆快速寫入較小且更快速的微服務。
* 效能較佳:.NET 6 是最快的完整堆疊 Web 架構,如果在雲端中執行,則可降低計算成本。
* 極致生產力:.NET 6 和 Visual Studio 2022 會提供熱重新載入、新 Git 工具、智慧型程式碼編輯、強固診斷和測試工具,及更好的小組共同作業。
### 講建立專案的部分前先看過Enity Framework core 與 LinQ
##
## Enity Framework core
### 是一個ORM框架可以大幅減少使用SQL語法且透過強型別取得與操作物件資料
#### 建立連線
```
private readonly SqlContext _context;
public Test_0612_01Controller(SqlContext context)
{
_context = context;
}
```
#### 新增資料
```
_context.表名稱.Add(遇新增的資料);
```
#### 刪除資料
```
_context.表名稱.FindAsync(id);
```
#### 儲存變更
```
_context.SaveChangesAsync();
```
#### 搜尋資料
##### 全部資料
```
_context.表名稱.ToListAsync();
```
##### 指定資料
```
_context.表名稱.FindAsync(id);
```
##### 條件式資料
```
_context.表名稱.Where(L=>L.條件式);
```
##
## LinQ(音link)
### 一種數據查詢語言被廣泛應用於C#中
### 對做API來說常常需要針對不同資料做提取這邊使用LinQ會方便很多
#### 查詢表名稱內所有資料
```
from c in _context.表名稱
select c
```
#### 查詢表名稱符合條件A的資料
```
from c in _context.表名稱
where 條件A
select c
```
#### 查詢表名稱內所有資料並由小至大
```
from c in _context.表名稱
orderby 欄位名
select c
```
##
## VS2022
### 安裝 [下載連接](https://visualstudio.microsoft.com/zh-hant/vs/compare/)
### 下載後若單純架設網站選擇ASP.NET 與 Node.js(之後寫Vue會用到)
### 若需要維護Winform 或是WPF 則選擇.NET桌面開發
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%871.png)
### 若需要快速建立則直接使用我提供的DEMO去做開發
2024-04-22 12:52:18 +08:00
##
2024-04-22 12:26:36 +08:00
# ASPNET
### 建立新專案
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%872.png)
### 選擇版本 (這邊以.NET6做教學 目前最新的.NET8)
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%873.png)
### 啟動
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%874.png)
### 安裝套件包(NuGet)
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%875.png)
### 設定資料庫連線
#### 於appsettings.json 中加入
```
//連接MYSQL
"ConnectionStrings": {
"tarefasConnection": "server=140.125.21.65;port=3307;uid=VIP125;pwd=@VIPvip125;database=VIP125"
}
```
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%876.png)
#### 於Program.cs中加入設定
```
builder.Services.AddDbContext<SqlContext>(opt =>
{
string connectionString = builder.Configuration.GetConnectionString("tarefasConnection");
var serverVersion = ServerVersion.AutoDetect(connectionString);
opt.UseMySql(connectionString, serverVersion);
});
```
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%877.png)
### 建立與資料庫table的連線
#### 創建Model
建議將創建的Model名稱第一個字大寫資料庫內部資料表名用小寫這樣較好對比
Model內容需要跟資料表的欄位名稱相對應
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%879.png)
#### 創建Services
Haskey指的是不會重複的
像是身份證字號這種的就不會重複
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%878.png)
### 創建控制器
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%8710.png)
### 創建出來的頁面
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%8711.png)
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%8712.png)
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%8713.png)
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%8714.png)
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%8715.png)
2024-04-22 12:52:18 +08:00
##
# WEBAPI
### 建立新專案
### 基本上都與ASPNET相同就這部分要注意
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%8717.png)
### 連接資料庫的方式都與ASPNET一樣這邊只是將ASPNET中的MVC去除View去更專注在後端這邊建立控制器的方式也有點不同
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%8718.png)
### 其餘的就好好看先前提到的ORM框架與LINQ去構建出API即可
### 運行之後:
![](http://140.125.21.65:8418/Education/Backend/raw/branch/master/C_shape/img/%E5%9C%96%E7%89%8719.png)