Model
Model 是系統「知道什麼」的部分,負責描述資料的結構與規則,是整個應用程式的資料核心
🎃 Seed
在開發或測試階段,我們常常會希望資料庫一開始就有資料方便測試,這時候就可以使用「Seed」方法。
1 | public class SchoolInitializer |
1 | // Add services to the container. |
🎃 OnModelCreating 是什麼?
OnModelCreating 是你告訴 EF:「這些資料表的細節,我要自己定義。」
你可以在這裡清楚告訴 EF:我要怎麼把 C# 的 class 對應到資料庫的 table 和欄位。
我們在繼承 DbContext 的資料上下文中 override 它
1 | protected override void OnModelCreating(ModelBuilder modelBuilder) |
這是 Entity Framework 提供的一個模型建構的「擴充點」,用來:
- 自訂資料表名稱、欄位名稱,這樣資料表就叫 StudentTable,欄位叫 StudentName,不會自動加 s 或亂推論。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public class Student
{
public int Id { get; set; }
public string FullName { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>(entity =>
{
entity.ToTable("StudentTable"); // 自訂資料表名稱
entity.Property(e => e.FullName)
.HasColumnName("StudentName"); // 自訂欄位名稱
});
} - 定義主鍵、索引、關聯(關聯式資料庫的關係)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33modelBuilder.Entity<Student>(entity =>
{
entity.HasKey(e => e.Id); // 設定主鍵欄位
});
modelBuilder.Entity<Student>(entity =>
{
entity.HasIndex(e => e.FullName); // 建立索引
});
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public List<Course> Courses { get; set; }
}
public class Course
{
public int Id { get; set; }
public string Title { get; set; }
public int StudentId { get; set; } // 外鍵
public Student Student { get; set; } // 導覽屬性
}
modelBuilder.Entity<Course>(entity =>
{
entity.HasOne(c => c.Student) // 一門課 有一個學生
.WithMany(s => s.Courses) // 一個學生 有很多課
.HasForeignKey(c => c.StudentId); // 外鍵欄位
});
🎃 DbContextOptions
是告訴 DbContext 要連哪個資料庫、用什麼方式連的「設定容器」。這段 constructor 會被 ASP.NET Core DI(依賴注入)自動呼叫,把連線資訊傳進來。
1 | public SchoolContext(DbContextOptions<SchoolContext> options) : base(options) |
LocalDB
LocalDB 是開發者用的輕量 SQL Server,他是開發測試用、免安裝完整版 SQL Server,可在本機開發、快速建立 DB
1 | "ConnectionStrings": { |
| 項目 | 意義 |
|---|---|
Server=(localdb)\MSSQLLocalDB |
指向本機 SQL Express 版 |
Trusted_Connection=True |
用目前 Windows 使用者登入,不需帳密 |
🎃 如何檢查 LocalDB 是否安裝
1 | sqllocaldb i |
🎃 連線伺服器 (Connect to Server)
Server name: (localdb)\MSSQLLocalDB
🎃 Index.cshtml view display 範例
1 | @model IEnumerable<ContosoUniversity.Models.Student> |
🎃 使用 Scaffold 自動產生 MVC Controller 和 View(搭配 Entity Framework)

- 在 Solution Explorer 中,右鍵點擊 Controllers 資料夾,選擇【Add > New Scaffolded Item(新增 Scaffold 項目)】。
- 在「Add Scaffold」對話框中,選擇:
- MVC 5 Controller with views, using Entity Framework(使用 Entity Framework 的 MVC 5 控制器與 View)
- 點選 [Add](新增),接著在「Add Controller」對話框中做設定
- 點擊 [Add](新增) 後,Scaffolder 會自動幫你產生相關內容
這些 View 搭配 Controller,就能完成一個基本的 CRUD 畫面。
🎃 參考教學
Tutorial: Get Started with Entity Framework 6 Code First using MVC 5Accessing Your Model’s Data from a New Controller
https://learn.microsoft.com/zh-tw/aspnet/mvc/overview/getting-started/introduction/accessing-your-models-data-from-a-controller
Examining the Edit Action Methods and Views for the Movie Controller
https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/examining-the-edit-methods-and-edit-view




