Keep your Entity Framework entities clean with EntityTypeConfiguration

What?

How?

Entity

public class Employee
{
    public int Id{ get; set; }
    public string FirstName{ get; set; }
    public string LastName{ get; set; }
    public string Email{ get; set; }
}

Mapping Class

public class EmployeeMap : EntityTypeConfiguration<Employee>
{
    public EmployeeMap()
    {
        ToTable("Employees");

        HasKey(x => x.Id);

        Property(x => x.Id).HasColumnName("Id");
        Property(x => x.FirstName).HasColumnName("FirstName");
        Property(x => x.LastName).HasColumnName("LastName");
        Property(x => x.Email).HasColumnName("Email");
    }
}

DbContext

public class MyDbContext : DbContext
{
    public MyDbContext() : base("ConnectionStringKey")
    {
    }

    protected override void OnModelCreating(DbModelBuilder builder)
    {
        builder
            .Configurations
            .AddFromAssembly(typeof (MyDbContext).Assembly);
        
        base.OnModelCreating(builder);
    }
}