Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 7 pluralize table names with code first approach

I am new to ASP/EF. I am using ASP 5 and Entity Framework 7 in my personal project.

So I am able to create database and tables with code first approach, but all the table names are singular and does not pluralize by default.

In my ServerMatrixDemoDB DbContext file I have created below DBSets:

public class ServerMatrixDemoDB : DbContext
{
    public ServerMatrixDemoDB()
    {
        Database.EnsureCreated();
    }
    public DbSet<Domain> Domains { get; set; }
    public DbSet<Environment> Environments { get; set; }
    public DbSet<Network> Networks { get; set; }
    public DbSet<OsVersion> OsVersions { get; set; }
    public DbSet<Tier> Tiers { get; set; }
    public DbSet<Service> Services { get; set; }
    public DbSet<HardwareType> HardwareTypes { get; set; }
    public DbSet<Powershell> Powershell { get; set; }
    public DbSet<DotNetVersion> DotNetVersions { get; set; }
    public DbSet<Status> Status { get; set; }
    public DbSet<Servers> Servers { get; set; }
    public DbSet<Application> Applications { get; set; }

}

And in my startup.cs file I am using below code:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()              // Add MVC Dependency.
        .AddJsonOptions(
            opt =>
            {
                opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); // Api convert all property names to CamelCase.
            }
        );
    services.AddLogging();          // Enable Logging for database errors.
    services.AddEntityFramework()
      .AddSqlServer()
      .AddDbContext<ServerMatrixDemoDB>(options =>
      {
          options.UseSqlServer(@"Server=.\SQLEXPRESS;user id=sa;password='passwrd';Database=ServerMatrixDemoDB;integrated security=True;");
      });
    services.AddTransient<ServerMatrixDemoSeedData>();
}

Once I build and run the project, a database and tables get created, but all table names are singular. Is there a way to pluralize table names in code first approach?

like image 427
Ray Avatar asked Jan 09 '16 23:01

Ray


1 Answers

you can do this in the OnModelCreating overload like -

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      foreach (var entity in modelBuilder.Model.GetEntityTypes())
      {
        modelBuilder.Entity(entity.Name).ToTable(entity.Name + "s");
      }
    }

you can also do this by using "data annotations"

    [Table("blogs")]
    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
    }

or Fluent Api

class MyContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Blog>()
                .ToTable("blogs");
        }
    }

for more details have a look at - Documentation for EF7

like image 59
Preet Singh Avatar answered Sep 23 '22 08:09

Preet Singh