EF Core Fluent API — Complete Guide
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.
Right
// 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
ApplyConfigurationsFromAssemblyto 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
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- 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
Learn more about EF Core Fluent API at DodaTech.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro