Configuring Entity Models with IEntityTypeConfiguration in EF Core

Entity Framework Core (EF Core) provides a flexible way to configure your entity models. One of the recommended approaches is to use the IEntityTypeConfiguration<T> interface, which allows you to separate your entity configurations into dedicated classes. This promotes cleaner code and better organization, especially in larger applications.

This guide demonstrates how to use IEntityTypeConfiguration to map an entity to a database table using a .NET Core console application and a MySQL database.

1. Define the Entity Model

First, create the C# class that represents your data model. For this example, we'll define a Course entity.

public class Course
{
    public int CourseId { get; set; }

    public string Title { get; set; }

    public int Credits { get; set; }
}

2. Implement the Configuration Class

Next, create a separate class that implements IEntityTypeConfiguration<Course>. This class will contain all the mapping logic for the Course entity.

public class CourseMap : IEntityTypeConfiguration<course>
{
    public void Configure(EntityTypeBuilder<course> builder)
    {
        // Map to the "Courses" table in the database
        builder.ToTable("Courses");

        // Define the primary key
        builder.HasKey(c => c.CourseId);

        // Configure properties
        builder.Property(c => c.CourseId).ValueGeneratedOnAdd();
        builder.Property(c => c.Title).IsRequired().HasMaxLength(50);
        builder.Property(c => c.Credits).IsRequired();
    }
}</course></course>

3. Create the DbContext

Now, define you're DbContext. In the OnModelCreating method, use the ApplyConfiguration method to register your mapping class.

public class UniversityContext : DbContext
{
    public DbSet<course> Courses { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // Configure the database provider (MySQL in this case)
        optionsBuilder.UseMySql(
            "Server=localhost;port=3306;database=university;uid=user;pwd=secret;",
            b => b.MaxBatchSize(30));
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        // Apply the configuration for the Course entity
        modelBuilder.ApplyConfiguration(new CourseMap());
    }
}</course>

4. Run Migrations

With the model and context defined, you can generate and apply migrations to create the database schema. Run the folllowing commands in your terminal:

dotnet ef migrations add InitialCreate
dotnet ef database update

5. Test Data Insertion

Finally, you can use the context to add and save data to the database.

public class App
{
    public static void Main(string[] args)
    {
        using (var context = new UniversityContext())
        {
            var newCourse = new Course
            {
                Title = "Introduction to Computer Science",
                Credits = 4
            };

            context.Courses.Add(newCourse);
            context.SaveChanges();
        }
    }
}

Tags: EntityFrameworkCore IEntityTypeConfiguration ModelBuilder MySQL

Posted on Thu, 23 Jul 2026 16:56:34 +0000 by danf_1979