What is "fluent-api"
Entity Framework Fluent API, also known only as Fluent API, is an entity configuration feature present in the Entity Framework, used to define characteristics of an entity, its properties or the relationship between entities.
There are two ways to use the Fluent API to configure a system:
1. Directly at the event OnModelCreating
of the class he inherits DbContext
public class MeuContexto : DbContext
{
...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Fluent API é usada aqui
base.OnModelCreating(modelBuilder);
}
...
}
2. Creating a mapping configuration class, which inherits EntityTypeConfiguration<T>
public class MinhaClasseDeConfiguracao : EntityTypeConfiguration<MeuModel>
{
public MinhaClasseDeConfiguracao()
{
// Fluent API é usada aqui
}
}
You need to register the configuration in the same event OnModelCreating
:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new MinhaClasseDeConfiguracao());
base.OnModelCreating(modelBuilder);
}
References: