Fluent API Configurations in EF 6

Entity Framework Fluent API is used to configure domain classes to override conventions. EF Fluent API is based on a Fluent API design pattern (a.k.a Fluent Interface) where the result is formulated by method chaining.

In Entity Framework 6, the DbModelBuilder class acts as a Fluent API using which we can configure many different things. It provides more options of configurations than Data Annotation attributes.

To write Fluent API configurations, override the OnModelCreating() method of DbContext in a context class, as shown below.

public class SchoolContext: DbContext 
{

    public DbSet<Student> Students { get; set; }
        
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //Write Fluent API configurations here

    }
}

You can use Data Annotation attributes and Fluent API at the same time. Entity Framework gives precedence to Fluent API over Data Annotations attributes.

Fluent API configures the following aspect of a model in Entity Framework 6:

  1. Model-wide Configuration: Configures the default Schema, entities to be excluded in mapping, etc.
  2. Entity Configuration: Configures entity to table and relationship mappings e.g. PrimaryKey, Index, table name, one-to-one, one-to-many, many-to-many etc.
  3. Property Configuration: Configures property to column mappings e.g. column name, nullability, Foreignkey, data type, concurrency column, etc.

The following table lists important Fluent API methods.

Configurations Fluent API Methods Usage
Model-wide Configurations HasDefaultSchema() Specifies the default database schema.
ComplexType() Configures the class as complex type.
Entity Configurations HasIndex() Configures the index property for the entity type.
HasKey() Configures the primary key property for the entity type.
HasMany() Configures the Many relationship for one-to-many or many-to-many relationships.
HasOptional() Configures an optional relationship which will create a nullable foreign key in the database.
HasRequired() Configures the required relationship which will create a non-nullable foreign key column in the database.
Ignore() Configures that the class or property should not be mapped to a table or column.
Map() Allows advanced configuration related to how the entity is mapped to the database schema.
MapToStoredProcedures() Configures the entity type to use INSERT, UPDATE and DELETE stored procedures.
ToTable() Configures the table name for the entity.
Property Configurations HasColumnAnnotation() Sets an annotation in the model for the database column used to store the property.
IsRequired() Configures the property to be required on SaveChanges().
IsConcurrencyToken() Configures the property to be used as an optimistic concurrency token.
IsOptional() Configures the property to be optional which will create a nullable column in the database.
HasParameterName() Configures the name of the parameter used in the stored procedure for the property.
HasDatabaseGeneratedOption() Configures how the value will be generated for the corresponding column in the database e.g. computed, identity or none.
HasColumnOrder() Configures the order of the database column used to store the property.
HasColumnType() Configures the data type of the corresponding column of a property in the database.
HasColumnName() Configures the corresponding column name of a property in the database.

Let's start to configure entities using Fluent API in the next chapter.