Skip to content

EF Core Fluent API — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about EF Core Fluent API. We cover key concepts, practical examples, and best practices.

You need to configure your EF Core model — table names, column types, indexes, relationships. Data annotations clutter your entity classes with attributes. The Fluent API provides a clean, centralized way to configure the model in code.

Wrong

[Table("tbl_Products")]
public class Product
{
    [Key]
    [Column("ProductId")]
    public int Id { get; set; }

    [Required]
    [MaxLength(200)]
    public string Name { get; set; }

    [Column(TypeName = "decimal(18,2)")]
    public decimal Price { get; set; }
}

Output: Works. But the entity class is cluttered with attributes. Configuration is spread across all entities.

// Entity — clean POCO
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

// DbContext — Fluent API
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Product>(entity =>
    {
        entity.ToTable("tbl_Products");
        entity.Property(e => e.Id).HasColumnName("ProductId");
        entity.Property(e => e.Name)
            .IsRequired()
            .HasMaxLength(200);
        entity.Property(e => e.Price)
            .HasColumnType("decimal(18,2)");
    });
}

Output: Same database schema. Clean entity classes. All configuration in one place.

For complex configurations, extract to separate configuration classes:

public class ProductConfiguration : IEntityTypeConfiguration<Product>
{
    public void Configure(EntityTypeBuilder<Product> builder)
    {
        builder.ToTable("tbl_Products");
        // ... configuration
    }
}

// In OnModelCreating:
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());

Prevention

  • Prefer Fluent API over data annotations for complex configurations.
  • Use Fluent API for relationships, indexes, and composite keys.
  • Use data annotations for simple constraints visible in the entity.
  • Extract configurations to IEntityTypeConfiguration<T> classes for large models.
  • Use ApplyConfigurationsFromAssembly to register all configurations automatically.
  • Use Fluent API for global query filters, owned entities, and value conversions.
  • Keep entity classes as clean POCOs for separation of concerns.

Common Mistakes with core fluent api

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world EF code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Can I mix Fluent API and data annotations?

Yes. Data annotations and Fluent API can coexist. Fluent API overrides data annotations if both specify the same property. Use annotations for simple rules and Fluent API for complex or overridden configurations.

What is the order of precedence?

Fluent API > data annotations > conventions. Fluent API settings always win. Data annotations override EF Core conventions. Conventions apply by default unless overridden by either method.

Should I configure everything in OnModelCreating or use separate files?

For large models, use separate IEntityTypeConfiguration<T> files — one per entity. For small models (fewer than 10 entities), inline configuration in OnModelCreating is fine. The separate file approach improves maintainability.

Learn more about EF Core Fluent API at DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro