
HTTP API vs WebSocket API:選擇哪個來實現實時通信?
Pokemon
── Owner
與 Pokemon
── Category
,都需通過中間表實現。在 Models
內新增 PokemonOwner.cs
:
public class PokemonOwner
{
public int PokemonId { get; set; }
public Pokemon Pokemon { get; set; }
public int OwnerId { get; set; }
public Owner Owner { get; set; }
}
同理創建 PokemonCategory.cs
:
public class PokemonCategory
{
public int PokemonId { get; set; }
public Pokemon Pokemon { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
}
Join 表同時持有兩個外鍵和對應導航屬性,用于存儲多對多關系。
Pokemon 類新增:
public ICollection < PokemonOwner > PokemonOwners { get; set; }
public ICollection < PokemonCategory > PokemonCategories { get; set; }
Owner 類新增:
public ICollection < PokemonOwner > PokemonOwners { get; set; }
Category 類新增:
public ICollection < PokemonCategory > PokemonCategories { get; set; }
通過集合導航屬性,EF Core 可識別并查詢關聯數據。
通過本篇,您已掌握手動構建 N\:N 關系的核心步驟,為更復雜的數據模型打下堅實基礎。
原文引自YouTube視頻:https://www.youtube.com/watch?v=oOUSvRc3FMo