четверг, 23 августа 2012 г.

Using the Code First Model Configuration Classes


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 theOnModelCreating 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 genericEntityTypeConfiguration and ComplexTypeConfiguration. Both of the classes live in theSystem.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 Courses { get; set; }
  public DbSet Departments { get; set; }

  #endregion

  #region Methods

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

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

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

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

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

    modelBuilder.Entity().
      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 and in its constructor, I’ll use the Fluent API for configurations. The following code sample shows the DepartmentTypeConfiguration class:
public class DepartmentTypeConfiguration : EntityTypeConfiguration
{
  #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 itsConfiguration 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 Courses { get; set; }
  public DbSet Departments { get; set; }

  #endregion

  #region Methods

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

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

    modelBuilder.Entity().
      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 theOnModelCreating 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 theModelConfiguration classes in order to divide your implementation to small objects with configuration responsibility.

Комментариев нет: