Skip to main content
deleted 12 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Code refactor for Entity Framework configuration

I tried to refactor my code responsabile with entity framework configuration, splitingsplitting 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);
}

Code refactor for Entity Framework configuration

I tried to refactor my code responsabile with entity framework configuration, spliting 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);
}

Entity Framework configuration

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);
}
Source Link
Dimitri
  • 221
  • 1
  • 12

Code refactor for Entity Framework configuration

I tried to refactor my code responsabile with entity framework configuration, spliting 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);
}