C# – EF Core Mapping EntityTypeConfiguration


In EF6 we usually able to use this way to configure the Entity.

public class AccountMap : EntityTypeConfiguration<Account>
    public AccountMap()
        HasKey(a => a.Id);

        Property(a => a.Username).HasMaxLength(50);
        Property(a => a.Email).HasMaxLength(255);
        Property(a => a.Name).HasMaxLength(255);

How we can do in EF Core, since when the class I Inherit EntityTypeConfiguration that unable to find the class.

I download the EF Core raw source code from the GitHub, I can't find it.
Can someone help on this?

Best Answer

Since EF Core 2.0 there is IEntityTypeConfiguration<TEntity>. You can use it like this:

class CustomerConfiguration : IEntityTypeConfiguration<Customer>
  public void Configure(EntityTypeBuilder<Customer> builder)
     builder.HasKey(c => c.AlternateKey);
     builder.Property(c => c.Name).HasMaxLength(200);

// OnModelCreating
builder.ApplyConfiguration(new CustomerConfiguration());

More on this and other new features introduced in 2.0 can be found here.