Disabling the pluralization of table names when using database migrations

To disable the pluralizing of database names create a custom class that inherits from the DBContext class. In that class override the OnModelCreating method and call the Remove method of the dbModelBuilder.Conventions class as below.

using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace DemoApplication.Models
{
    public class EFDbContext : DbContext
    {
        //the base contains the name of the connection string provided in the web.config
        // this constructor should disable migrations
        public EFDbContext()
            : base("EFDbContext")
        {}

        protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
        {
            dbModelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

        public DbSet<Query> Query { get; set; }
    }
}

2 thoughts on “Disabling the pluralization of table names when using database migrations

    • Dave's avatar Dave December 10, 2020 / 10:53

      For individual tables this can be done either via the Table Attribute on the model class

      [Table(string name, Properties:[Schema = string])
      
      // example
      [Table("Student", Schema="Admin")]
      

      or by using FluenApi

      modelBuilder.Entity().Totable("Student");
      

      If you are using FluentApi and there are a lot of entitites (table models) the configuration code can get quite long in the OnModelCreating method of the context. I would recommend having a configuration class for each entity to hold the FluentApi configuration and reference these configuration classes in the OnModelCreating method. The configuration class should derive from EntityTypeConfiguration as in the example below.
      The below example is from entityframeworktutorial.net

      public class StudentEntityConfiguration: EntityTypeConfiguration
      {
          public StudentEntityConfiguration()
          {
                  this.ToTable("StudentInfo");
                  this.HasKey(s => s.StudentKey);
                  this.Property(p => p.DateOfBirth)
                          .HasColumnName("DoB")
                          .HasColumnOrder(3)
                          .HasColumnType("datetime2");
      
                  this.Property(p => p.StudentName)
                          .HasMaxLength(50);
                              
                  this.Property(p => p.StudentName)
                          .IsConcurrencyToken();
                      
                  this.HasMany(s => s.Courses)
                      .WithMany(c => c.Students)
                      .Map(cs =>
                              {
                                  cs.MapLeftKey("StudentId");
                                  cs.MapRightKey("CourseId");
                                  cs.ToTable("StudentCourse");
                              });
          }
      }
      

      then in the OnModelCreating of your context class

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

Leave a Reply