Fluent API is an advanced way of specifying model configuration that covers everything that data annotations can do and some more advanced configuration not possible with data annotations. Data annotations and the fluent API can be used together, but Code First gives precedence to Fluent API > data annotations > default conventions.
Fluent API provides several essential methods to configure entities and their properties to override various Code First conventions. Below are some of them.
S.No. | Method Name & Description |
---|---|
1. | ComplexType TComplexType> Registers a type as a complex type in the model and returns an object that can be used to configure the complex type. This method can be called multiple times for the same type to perform various configuration lines. |
2. | Entity Registers an entity type as part of the model and returns an object that can be used to configure the entity. This method can be called multiple times for the same entity to perform various configuration lines. |
3. | HasKey Configures the primary fundamental property(s) for this entity type. |
4. | HasMany Configures many relationships from this entity type. |
5. | HasOptional Configures an optional relationship from this entity type. Instances of the entity type will be able to be saved to the database without this relationship being specified, and the foreign key in the database will be nullable. |
6. | HasRequired 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. |
7. | Ignore Excludes a property from the model so that it will not be mapped to the database. (Inherited from StructuralTypeConfiguration |
8. | Property Configures a struct property that is defined on this type. (Inherited from StructuralTypeConfiguration |
9. | ToTable(String) Configures the table name that this entity type is mapped to. |
Fluent API lets you configure your entities or their properties, whether you want to change something about how they map to the database or how they relate to one another. There's a massive variety of mappings and modeling that you can impact using the configurations. Following are the main types of mapping that Fluent API supports.
Entity mapping is just some simple mappings that will impact the Entity Framework's understanding of how the classes are mapped to the databases. All these we discussed in data annotations, and here we will see how to achieve the same things using Fluent API.
The default schema is dbo when the database is generated. You can use the HasDefaultSchema method on DbModelBuilder to specify the database schema for all tables, stored procedures, etc.
With default convention, Code First will create the database tables with the name of DbSet properties in the context class such as Courses, Enrollments, and Students. But if you want different table names, you can override this convention and provide another table name other than the DbSet properties.
Entity Splitting lets you combine data from multiple tables into a single class, and it can only be used with tables that have a one-to-one relationship between them.
The Property method is used to configure attributes for each property belonging to an entity or complex type. The Property method obtains a configuration object for a given property. You can also map and configure the properties of your domain classes using Fluent API.
The default convention for primary keys is −
In Entity Framework, Code First will create a column for a property with the same name, order, and datatype.
The Course Title property should be no longer than 24 characters in the following example. When the user specifies a value longer than 24 characters, the user will get a DbEntityValidationException exception.
In the context of databases, a relationship exists between two relational database tables when one table has a foreign key that references the primary key of the other table. When working with Code First, you define your model by defining your domain CLR classes. By default, the Entity Framework uses the Code First conventions to map your courses to the database schema.
When you define a one-to-one relationship in your model, you use a reference navigation property in each class. Both tables can have only one record on either side of the relationship in the database. Each Primary key value relates to only one record (or no records) in the related table.
The primary key table contains only one record that relates to none, one, or many records in the related table. This is the most commonly used type of relationship.
Each record in both tables can relate to any number of records (or no records) in the other table.
|