Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I tried to refactor my code with entity framework configuration, splitting in multiple private methods, one per entity, each responsible for configuring things related to that entity and only that entity. But I feel like Version 2 can still be improved.

What do you think?

Version 1:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Country>()
        .Property(e => e.Code)
        .IsFixedLength()
        .IsUnicode(false);

    modelBuilder.Entity<Country>()
        .Property(e => e.PhoneCode)
        .IsFixedLength()
        .IsUnicode(false);

    modelBuilder.Entity<Country>()
        .HasMany(e => e.Locations)
        .WithRequired(e => e.Country)
        .WillCascadeOnDelete(false);
}

Version 2:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    Configure(modelBuilder.Entity<Country>());
}

Configure(EntityTypeConfiguration<Country> entity)
{
    entity
        .Property(e => e.Code)
        .IsFixedLength()
        .IsUnicode(false);

    entity  
        .Property(e => e.PhoneCode)
        .IsFixedLength()
        .IsUnicode(false);

    entity
        .HasMany(e => e.Locations)
        .WithRequired(e => e.Country)
        .WillCascadeOnDelete(false);
}
share|improve this question

My approach is to keep the XxxxxDbContext class as lightweight as possible:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    new ModelCreator().Execute(modelBuilder);
}

The ModelCreator class looks like this:

internal class ModelCreator
{
    public void Execute(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

        modelBuilder.Configurations.Add(new FirmConfiguration());
        // etc.
    }        
}

Then each table has its own EntityTypeConfiguration<TEntityType>:

internal class FirmConfiguration : EntityTypeConfiguration<Firm>
{
    public FirmConfiguration()
    {
        HasKey(x => new { x.Id });

        Property(t => t.Name).HasMaxLength(30).IsRequired();
        Property(t => t.Address).HasMaxLength(120).IsRequired();
        Property(t => t.PostalCode).HasMaxLength(4).IsRequired();
        Property(t => t.City).HasMaxLength(30).IsRequired();
        Property(t => t.Responsible).HasMaxLength(30).IsRequired();
        Property(t => t.Email).HasMaxLength(40).IsRequired();
        Property(t => t.Phone).HasMaxLength(14);
    }
}
share|improve this answer
1  
FWIIW, There's a method on the DbModelBuilder that scans an assembly for EntityTypeConfiguration<TEntityType>s: modelBuilder.Configurations.AddFromAssembly(Assembly.GetExec‌​utingAssembly());. You can also do something like typeof(FirmConfiguration).Assembly if the configs aren't in same assembly as the context. – RobH Aug 24 at 9:42
    
@RobH Cool, didn't know that. – BCdotWEB Aug 24 at 9:48
    
this is how I'm configuring entities too. I'm coming from nhibernate and I have a number of mapping classes and pocos. this methods allows me to keep my domain objects clean while also mapping to EF. – Fran Aug 24 at 13:43

I prefer to use generics for the other method:

private void Configure<T>(DbModelBuilder modelBuilder)
{
    var entity = modelBuilder.Entity<T>();

    entity
        .Property(e => e.Code)
        .IsFixedLength()
        .IsUnicode(false);
    ...
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    Configure<Country> (modelBuilder);
}

or you can make it an extension:

static DbModelBuilder Configure<T>(this DbModelBuilder modelBuilder)
{
    var entity = modelBuilder.Entity<T>();

    entity
        .Property(e => e.Code)
        .IsFixedLength()
        .IsUnicode(false);

    ...

    return modelBuilder;
}

and chain the calls:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder
        .Configure<Country>()
        .Configure<OtherModel>();
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.