Entity Framework Core Interview Question #11

What is the Fluent API in EF Core?

EF Core Fundamentals & DbContext Junior Beginner

Quick Interview Answer

The Fluent API configures the EF Core model through ModelBuilder, usually inside OnModelCreating or IEntityTypeConfiguration classes.

Detailed Explanation

The Fluent API configures the EF Core model through ModelBuilder, usually inside OnModelCreating or IEntityTypeConfiguration classes.

It can configure keys, indexes, column types, relationships, delete behavior, conversions, defaults, constraints, table mappings, and many other model details.

Fluent configuration is especially useful for complex applications because persistence mapping can be kept separate from the entity classes.

Code Example

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Product>(entity =>
    {
        entity.HasKey(p => p.Id);
        entity.Property(p => p.Name)
              .HasMaxLength(200)
              .IsRequired();
    });
}