前言
假設 Entity Class 要提供 Dictionary 的屬性,
可以使用 Indexer Properties,例如,customer["CustomerType"]
。
如果不想在原有的 Table 中新增欄位,
則可以使用 property bag entity types,例如,customerExt["CustomerType"]
以下我們來看一下做法。
實作
Indexer Properties
customer["CustomerType"]
1.在 Customer Class 中加入 Dictionary
1 | public class Customer |
2.在 DbContext 類別中 OnModelCreating Method 設定 IndexerProperty,
1 | public partial class 你的Context: DbContext |
所以當 update-database
後,Customers 會增加 2 個欄位(LastUpdated 及 CustomerType)

3.存/取值的方式如下,
1 | var _dbContext = new 你的Context(); |
Property bag entity types
存到另一個 Class ,例如,customerExt["CustomerType"]
1.建立 DbSet<Dictionary<string, object>>
及設定 SharedTypeEntity
例如建立 Customers 及 Orders 的擴充 Table(CustomerExts, OrderExts)
1 | public partial class 你的Context: DbContext |
2.存取方式如下,
1 | var _dbContext = new 你的Context(); |
DB 就會建立 CustomerExts 及 OrderExts 資料表,

