我們已經在code-first 約定一文中,已經知道了Code-First為每一個具體的類,創建數據表。
但是你可以自己利用繼承設計領域類,面向對象的技術包含“has a”和“is a”的關系即,有什么,是什么,的關系,但是基于SQL關系的實體和數據表集合之前,只是有“has a ”的關系。數據庫管理系統(SQL database management systems)不支持繼承類型,所以,你怎么將面向對象的領域實體和關系型的數據庫映射在一起呢???
下面有三種方式,在Code-First中表現繼承:
- Table per Hierarchy (TPH): This approach suggests one table for the entire class inheritance hierarchy. Table includes discriminator column which distinguishes between inheritance classes. This is a default inheritance mapping strategy in Entity Framework.
這種方式,推薦一個表作為整個類的繼承層次結構。這個表包含監聽列可以和繼承類之間很好的區分開來。這個是EF中默認的繼承策略。
- Table per Type (TPT): This approach suggests a separate table for each domain class.
這個方式,推薦給每一個領域類創建一個單獨的表。
- Table per Concrete class (TPC): This approach suggests one table for one concrete class, but not for the abstract class. So, if you inherit the abstract class in multiple concrete classes, then the properties of the abstract class will be part of each table of the concrete class.
這個方式,推薦為每一個具體的類,創建一個表,但是抽象類除外。因此如果你在多個具體的類中,繼承了抽象類的話,那么這個抽象類的屬性將會是每一個具體的類(表)的屬性的一部分。
我們在這里不做過多深入的了解,如果想了解更深入的技術,請看:
We are not going into detail here. Visit the following reference link for more detailed information:
- Inheritance with EF Code First: Table per Hierarchy (TPH)
- Inheritance with EF Code First: Table per Type (TPT)
- Inheritance with EF Code First: Table per Concrete class (TPC)
We have seen default Code First conventions in the previous section. Learn how to configure domain classes in order to override these conventions in the next section.
在之前的環節中,我們已經看到了Code-First默認約定,下面一節,讓我們來學一下怎么來配置我們的領域類,為了能夠打破默認約定。
PS:這個繼承的部分,我后面會深入看下里面的技術,然后翻譯出來。請大家多多支持!!!
附上目錄:
- 什么是Code First
- 簡單的Code First例子
- Code-First 約定
- DB Initialization(數據庫初始化)
- Inheritance Strategy(繼承策略)
- Configure Domain Classes(配置領域類)
- DataAnnotations(數據注解)
- Fluent API
- Configure One-to-One(配置一對一關系)
- Configure One-to-Many(配置一對多關系)
- Configure Many-to-Many(配置多對多關系)
- Move Configurations(數據遷移)
- DB Initialization Strategy(數據庫初始化策略)
- ...待續.....
文章列表