返回筆記列表
/ 筆記詳情
老師資源¶
https://flipclass.stust.edu.tw/media/doc/345156
取出所有客戶資料¶
Index動作¶
public IActionResult Index(string city)
{
using (var ctx=new NorthwindDBContext())
{
var cities = ctx.Customers.Select(x => x.City).Distinct().ToList();
var customers = ctx.Customers.Select(x=>new { 公司名稱=x.CompanyName, x.Address, x.City, x.ContactName, x.Phone }).Where(x=>x.City==city).ToList();
ViewBag.customers = customers;
ViewBag.cities = cities;
return View();
}
}
Index畫面¶
<div class="text-center">
<div>
<form action="~/Home/Index" method="post">
縣市
<select name="city">
@foreach (var d in cities)
{
<option value="@d">@d</option>
}
</select>
<button type="submit" class="btn btn-primary">選擇</button>
</form>
</div>
<div class="container w-75">
<table class="table">
<thead>
<tr>
<th scope="col">公司名稱</th>
<th scope="col">聯絡人</th>
<th scope="col">縣市</th>
<th scope="col">地址</th>
<th scope="col">電話</th>
</tr>
</thead>
<tbody>
@foreach (var c in customers)
{
<tr>
<td>@c.公司名稱</td>
<td>@c.ContactName</td>
<td>@c.City</td>
<td>@c.Address</td>
<td>@c.Phone</td>
</tr>
}
</tbody>
</table>
</div>
</div>
1. 開啟資料庫伺服器¶
選擇專案 檢視-SQL SERVER物件總管
點選SQL SERVER 按下滑鼠右鍵 選擇 加入SQL Server 或中斷連接
2. 建立資料庫(使用sql命令)¶
點選 SQL SERVER 按下滑鼠右鍵 選擇 新增查詢
將附檔Northwindsql命令內容複製並貼於新增查詢頁面 後執行
3. 建立資料庫(使用附加方式)¶
選擇 檢視-資料庫總管-資料連接-加入連接-MS SQL Server資料庫檔案
4. 在專案安裝 Entity Framework套件¶
Entity Framework(又名ADO.NET Entity Framework)是微軟以ADO.NET為基礎所發展出來的物件關聯對應(O/R Mapping)的解決方案,使用者不直接與資料庫互動,而是藉由Entity Framework物件與資料庫進行互動。
請開啟 工具-NuGet套件管理員-管理方案NuGet套件,點選瀏覽並安裝下列二項套件(因為電腦教室使用 .Net 8.0 sdk,請選擇 9.0.xx版本)-如果要使用最新版請更新 .Net SDK:
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Tools
5. 執行Entity Framework套件命令建立對應資料庫資料表資料類型及連接 Context 檔案¶
選擇 工具-NuGet套件管理員-套件管理器主控台,並輸入
Scaffold-DbContext -Connection "Server=資料庫伺服器;Database=資料庫名稱;Trusted_Connection=True; User ID=資料庫伺服器名稱;Password=資料庫伺服器密碼" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force -Context 資料庫名稱DBContext
- 若資料庫伺服器為
(localdb)\MSSQLLocalDB - 資料庫名稱為 Northwind
- 本地端登入帳號可移除 User ID 及 Password
-OutputDir設定資料表資料模型檔案輸出位置-Context用於設定資料庫連接檔案名稱
範例:
Scaffold-DbContext -Connection "Server=(localdb)\MSSQLLocalDB;Database=Northwind;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force -Context NorthwindDBContext
6. 資料庫操作¶
控制器-動作使用下列命令連接並執行資料庫操作:
using (var ctx=new 資料庫Context())
{
var c = ctx.資料模型.ToList();
return View();
}
範例:取得所有客戶資料並轉為清單存在變數 c:
using (var ctx = new NorthwindDBContext())
{
var c = ctx.Customers.ToList();
return View(c);
}
練習¶
I Index動作¶
public IActionResult Index()
{
using (var ctx=new NorthwindDBContext())
{
List<Customer> c = ctx.Customers.ToList();
return View(c);
}
}
Index.cshtml 檢視頁面¶
<div class="container w-75">
<table class="table">
<thead>
<tr>
<th scope="col">名稱</th>
<th scope="col">縣市</th>
<th scope="col">地址</th>
<th scope="col">聯絡人</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
1. 請取得所有產品資料 (Products)¶
Index動作¶
public IActionResult Index()
{
using (var ctx=new NorthwindDBContext())
{
var p = ctx.Products.ToList();
return View(p);
}
}
Index畫面¶
@model List<WebApplication1.Models.Product>
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<div class="container">
<table class="table">
<thead>
<tr>
<th scope="col">產品名稱</th>
<th scope="col">供應商編號</th>
<th scope="col">單價</th>
<th scope="col">庫存</th>
</tr>
</thead>
<tbody>
@foreach (var p in Model)
{
<tr>
<th scope="row">@p.ProductName</th>
<td>@p.SupplierId</td>
<td>@p.UnitPrice</td>
<td>@p.UnitsInStock</td>
</tr>
}
</tbody>
</table>
</div>
</div>
2. 請取得所有訂單資料 (Orders)¶
3. 請取得所有員工資料 (Employees)¶
Index動作¶
public IActionResult Index()
{
using (var ctx = new NorthwindDBContext())
{
var c = ctx.Employees.ToList();
return View(c);
}
}
Index檢視檔¶
@model List<WebApplication2.Models.Employee>
@{
ViewData["Title"] = "Home Page";
}
<div class="container">
<div class="row">
@foreach (var e in Model)
{
<div class="col-4">
<div class="card">
<img src="~/imgs/pic.jpg" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">@e.LastName, @e.FirstName</h5>
<p class="card-text">@e.Address</p>
<p class="card-text">@e.HomePhone</p>
</div>
</div>
</div>
}
</div>
</div>
選取部分欄位¶
透過 Select 命令設定取得欄位,請注意 x=> 為 Lambda 表示式,x 代表資料表每一筆紀錄。
下列命令取出產品資料表 ProductName 與 UnitPrice 欄位:
using (var ctx = new NorthwindDBContext())
{
var c = ctx.Products.Select(x=>new {x.ProductName,x.UnitPrice}).ToList();
return View();
}
修改選取欄位名稱(在欄位前設定新名稱)¶
using (var ctx = new NorthwindDBContext())
{
var c = ctx.Products.Select(x=>new {產品名稱=x.ProductName,x.UnitPrice}).ToList();
return View();
}
選取計算欄位¶
using (var ctx = new NorthwindDBContext())
{
var c = ctx.OrderDetails.Select(x=>new {x.Quantity,x.UnitPrice,x.Discount, 金額=Convert.ToSingle(x.Quantity)*Convert.ToSingle(x.UnitPrice)*(1-x.Discount)}).ToList();
return View();
}
練習¶
- 取出客戶資料表 Customers 中 CustomerName, City, Country 欄位
- 取出員工資料表 Employees 中 LastName, FirstName, HomePhone 欄位
context.資料表.First()¶
context.資料表.Select(x=>x.欄位)¶
public IActionResult Index()
{
using (var ctx = new NorthwindDBContext())
{
var c = ctx.Customers.Select(x => x.Country).ToList();
return View(c);
}
//return View();
}
public IActionResult Index()
{
using (var ctx = new NorthwindDBContext())
{
var c = ctx.Customers.Select(x => new { x.Country, x.City}).ToList();
return View(c);
}
//return View();
}
public IActionResult Index()
{
using (var ctx = new NorthwindDBContext())
{
var c = ctx.Customers.Select(x => x.Country).Distinct().ToList();
return View(c);
}
//return View();
}
context.資料表.Where(x=>x.欄位==‘值’)¶
德國(Germany)客人:
using (var ctx=new NorthwindDBContext())
{
var c = ctx.Customers.Where(x=>x.Country=="Germany").ToList();
return View(c);
}
context.資料表.OrderBy(x=>x.欄位)¶
context.資料表.GroupBy(x=>x.欄位)¶
context.資料表.GroupBy(x=>new {名稱=x.欄位})¶
context.資料表.OrderBy(x=>x.欄位1).ThenOrderBy(x=>x.欄位2)¶
context.資料表.ToList()¶
public IActionResult Index()
{
using (var ctx = new NorthwindDBContext())
{
var c = ctx.Employees.ToList();
return View(c);
}
//return View();
}
Razor 範例¶
@model List<Employee>
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
@foreach (Employee e in Model)
{
<h3>@e.FirstName</h3>
}
</div>
資料庫 CRUD 方法與用法¶
| 方法 | Usage |
|---|---|
| Add | 新增必須執行 SaveChanges() |
| AddAsync | 新增必須執行 SaveChanges() |
| AddRange | 新增必須執行 SaveChanges() |
| AddRangeAsync | 新增必須執行 SaveChanges() |
| Remove | 移除必須執行 SaveChanges() |
| RemoveRange | 移除必須執行 SaveChanges() |
| SaveChanges | 儲存 |
| SaveChangesAsync | 非同步儲存 |
| Update | 更新必須執行 SaveChanges() |
| UpdateRange | 更新必須執行 SaveChanges() |
Join 範例 1¶
using (var context = new BookStore())
{
var data = context.Authors
.Join(
context.Books,
author => author.AuthorId,
book => book.Author.AuthorId,
(author, book) => new
{
BookId = book.BookId,
AuthorName = author.Name,
BookTitle = book.Title
}
).ToList();
}
Join 範例 2¶
using (var context = new BookStore())
{
var authorsData = context.Authors
.Join(
context.AuthorBiographies,
author => author.AuthorId,
authorBio => authorBio.AuthorBiographyId,
(author, authorBio) => new
{
AuthorId = author.AuthorId,
Name = author.Name,
Biography = authorBio.Biography
}
)
.Join(
context.Books,
author => author.AuthorId,
book => book.BookId,
(author, book) => new
{
AuthorId = author.AuthorId,
Name = author.Name,
Biography = author.Biography,
BookTitle = book.Title
}
)
.ToList();
}
以上即為您的上課資源內容已轉為Markdown格式。若需進一步協助,歡迎告知。
來源
附件 248
WebApplicationEFCore
(1)
WebApplicationEFCore
(6)
wwwroot
(1)
lib
(0)
jquery-validation-unobtrusive
(3)
jquery-validation
(1)
jquery
(1)
bootstrap
(1)
dist
(0)
js
(12)
css
(32)
Views
(2)
Shared
(4)
obj
(5)
Debug
(0)
net8.0
(22)
staticwebassets
(4)
scopedcss
(0)
Models
(10)
bin
(0)
Debug
(0)
net8.0
(62)
zh-Hant
(5)
zh-Hans
(4)
tr
(4)
runtimes
(0)
win
(0)
lib
(0)
net6.0
(6)
ru
(4)
pt-BR
(4)
pl
(4)
ko
(3)
ja
(3)
fr
(3)
es
(3)
de
(2)
vs
(0)
WebApplicationEFCore
(0)
FileContentIndex
(2)