65.9K
CodeProject is changing. Read more.
Home

Using the Code First Model Configuration Classes

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (7 votes)

Mar 7, 2011

CPOL

2 min read

viewsIcon

83010

Aother model configuration option which is built inside Code First.

Introduction

In the past, I explained how to use the Code First Fluent API in order to configure and shapeUsing Code First Model Configurations Classes your EDM during runtime. One of the problems that might arise when you use the Fluent API as I showed in the previous post is that the OnModelCreating method might become bloated and hard to read. This is the time to get familiar with another model configuration option which is built inside Code First.

Model Configurations Classes

When you use Code First, you will probably configure the creation of the model in some way. You can use the Code First Fluent API in order to do that. When you use the Fluent API, the place that you will use it is the OnModelCreating method in the DbContext class. In very big models, that might be a problem. Quickly, you will find yourself having a very big and bloated method which holds all the configurations. This is the time for refactoring your code to use model configurations. There are two major classes that you will use: the generic EntityTypeConfiguration and ComplexTypeConfiguration. Both of the classes live in the System.Data.Entity.ModelConfiguration assembly.

Usage Example

Let's revisit the DbContext from the previous post:

public class SchoolEntities : DbContext
{
  #region Ctor 

  public SchoolEntities() :
    base("MySchool")
  {
  }

  #endregion

  #region Properties

  public DbSet<Course> Courses { get; set; }
  public DbSet<Department> Departments { get; set; }

  #endregion

  #region Methods

  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Department>().
      Property(d => d.Name).
      IsRequired().
      HasMaxLength(50);

    modelBuilder.Entity<Department>().
      Property(d => d.DepartmentID).
      HasDatabaseGenerationOption(DatabaseGenerationOption.None);

    modelBuilder.Entity<Department>().
      HasMany(d => d.Courses).
      WithRequired(c => c.Department).
      HasForeignKey(c => c.DepartmentID).
      WillCascadeOnDelete();

    modelBuilder.Entity<Department>().
      Ignore(d => d.Administrator);

    modelBuilder.Entity<Course>().
      Property(c => c.Title).
      IsRequired().
      HasColumnName("Name");      
  }    

  #endregion
}

If I want to use the EntityTypeConfiguration, I’ll create a new class for the Department configuration. The class will inherit from EntityTypeConfiguration<Department> and in its constructor, I’ll use the Fluent API for configurations. The following code sample shows the DepartmentTypeConfiguration class:

public class DepartmentTypeConfiguration : EntityTypeConfiguration<Department>
{
  #region Ctor

  public DepartmentTypeConfiguration()
  {
    Property(d => d.Name).
      IsRequired().
      HasMaxLength(50);

    Property(d => d.DepartmentID).
      HasDatabaseGenerationOption(DatabaseGenerationOption.None);

    HasMany(d => d.Courses).
      WithRequired(c => c.Department).
      HasForeignKey(c => c.DepartmentID).
      WillCascadeOnDelete();

    Ignore(d => d.Administrator);
  }

  #endregion
}

Now that we have the class, we will wire it into the ModelBuilder by using the Add method of its Configuration collection. The following code sample shows you how to do that:

public class SchoolEntities : DbContext
{
  #region Ctor

  public SchoolEntities() :
    base("MySchool")
  {
  }

  #endregion

  #region Properties

  public DbSet<Course> Courses { get; set; }
  public DbSet<Department> Departments { get; set; }

  #endregion

  #region Methods

  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    base.OnModelCreating(modelBuilder);

    modelBuilder.Configurations.Add(new DepartmentTypeConfiguration());

    modelBuilder.Entity<Course>().
      Property(c => c.Title).
      IsRequired().
      HasColumnName("Name");
  }

  #endregion
}

During runtime, we will get the same EDM whether we use the first method with the configurations inside the OnModelCreating method or the second method of EntityTypeConfiguration.

Summary

When you use Code First, you get a lot of configuration options. If your model is small, you can create all the configurations inside the OnModelCreating method. When the model starts to grow, you can use the ModelConfiguration classes in order to divide your implementation to small objects with configuration responsibility.