Entitytypeconfiguration Class in Code-first:
Before we start to configure using the Fluent API, let's see an important class of fluent API.
Entitytypeconfiguration is a important class in Fluent API. Entitytypeconfiguration provides important methods to configure entities and its properties to override various Code-f IRST conventions. It can be obtained by calling the Entity<tentity> () method of Dbmodelbuilder class as shown below.
Entitytypeconfiguration has the following important methods:
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 the this entity type. |
Hasoptional<ttargetentity> |
Optionalnavigationpropertyconfiguration |
Configures an optional relationship from the 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 would be nullable. |
Hasrequired<ttargetentity> |
Requirednavigationpropertyconfiguration |
Configures a required relationship from the this entity type. Instances of the entity type are not being able to being saved to the database unless this relationship is specified. The foreign key in the database would be non-nullable. |
Ignore<tproperty> |
Void |
Excludes a property from the model so, it would not be mapped to the database. |
Map |
Entitytypeconfiguration |
Allows advanced configuration related to how this entity type is mapped to the database schema. |
Property<t> |
Structuraltypeconfiguration |
Configures a struct property, which is defined on the this type. |
ToTable |
Void |
Configures the table name, the This entity type was mapped to. |
Visit MSDN For more information on entitytypeconfiguration class.
Let's start to configure entities using Fluent API in the next section.
Entity Framework Code-first (10.1): Entitytypeconfiguration