在我們學習Fluent API之前,先來看看Fluent API中重要的類--EntityTypeConfiguration吧。
EntityTypeConfiguration類是Fluent API 中重要的類,這個類提供了重要的屬性和方法來配置領域類,讓我們可以不用按照約定的Code-First那樣的配置來,配置我們的領域類。
EntityTypeConfiguration類型可以通過調用DbModelBuilder類中的Entity<TEntity>()泛型方法得到。
EntityTypeConfiguration類,有下面比較重要的方法。
Method Name | Return Type | Description |
---|---|---|
HasKey<TKey> | EntityTypeConfiguration | Configures the primary key property(s) for this entity type.【配置主鍵】 |
HasMany<TTargetEntity> | ManyNavigationPropertyConfiguration | Configures a many-to-many relationship from this entity type.【配置多對多的關系】 |
HasOptional<TTargetEntity> | OptionalNavigationPropertyConfiguration |
Configures an optional relationship from this entity type. Instances of the entity type can be saved to the database without this relationship being specified. The foreign key in the database will be nullable. 【配置可選的關系,實體的類型可以被保存到數據庫,不用特別指定關系,數據庫中的外鍵列將會是可空的。】 |
HasRequired<TTargetEntity> | RequiredNavigationPropertyConfiguration |
Configures a required relationship from this entity type. Instances of the entity type will not be able to be saved to the database unless this relationship is specified. The foreign key in the database will be non-nullable. 【配置必須關系,實例不會保存到數據庫,除非關系指定。數據庫的外鍵列,將會是不能為空的。】 |
Ignore<TProperty> | Void |
Excludes a property from the model so that it will not be mapped to the database. 【標注了這個Ignore特性的屬性,不會映射到數據庫中。】 |
Map | EntityTypeConfiguration |
Allows advanced configuration related to how this entity type is mapped to the database schema. 【允許高級的配置將這個實體類型映射成數據表】 |
Property<T> | StructuralTypeConfiguration |
Configures a struct property that is defined on this type. 【配置屬性】 |
ToTable | Void |
Configures the table name that this entity type is mapped to. 【配置實體,映射成的表名】 |
要了解更多關于這個EntityTypeConfiguration類的信息,請看MSDN, EntityTypeConfiguration。
文章列表