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);
}
2 Answers 2
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);
}
}
-
1\$\begingroup\$ FWIIW, There's a method on the
DbModelBuilder
that scans an assembly forEntityTypeConfiguration<TEntityType>
s:modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
. You can also do something liketypeof(FirmConfiguration).Assembly
if the configs aren't in same assembly as the context. \$\endgroup\$RobH– RobH2016年08月24日 09:42:43 +00:00Commented Aug 24, 2016 at 9:42 -
\$\begingroup\$ @RobH Cool, didn't know that. \$\endgroup\$BCdotWEB– BCdotWEB2016年08月24日 09:48:38 +00:00Commented Aug 24, 2016 at 9:48
-
\$\begingroup\$ 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. \$\endgroup\$Fran– Fran2016年08月24日 13:43:10 +00:00Commented Aug 24, 2016 at 13:43
Consider splitting the Configure
method into smaller pieces. Each piece should configure only one entity at a time.
You could write them as extensions so that you an chain them:
static DbModelBuilder ConfigureCountry(this DbModelBuilder modelBuilder)
{
var entity = modelBuilder.Entity<Country>();
entity
.Property(e => e.Code)
.IsFixedLength()
.IsUnicode(false);
..
return modelBuilder;
}
inside OnModelCreating
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder
.ConfigureCountry()
.ConfigureAnotherEntity();
}