Entity Framework Code First naming conventions - back to plural table names?

The RTM version of Code First will fully support a cool feature called Pluggable Conventions where you can add or replace the default conventions such as the one you mentioned.

Fortunately, what you are looking for is already included in CTP5. You can switch off the pluralizing table names convention with removing PluralizingTableNameConvention convention. This is all the code you need to write for this matter:

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

protected override void OnModelCreating(ModelBuilder modelBuilder)
{    
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}

About your secound questions, the reason you more see Northwind database than Adventure Works is just because AW is a huge and Northwind is a fairly small database hence make it a better fit for samples and Walkthroughs. That said, you still need to write some code to work with Northwind database in Code First.


here is one extract of the code I'm using and is working 100%. Try copy and paste and try it, it must create singular name table names. I'm using EF4.1 and CE4.0

POCO class

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace CloudOne.Models
{
    public class Brand
    {
        public int BrandID { get; set; }
        [MaxLength(25)]
        [Required]
        public string BrandName { get; set; }
        [MaxLength(1000)]
        public string BrandDescription { get; set; }
        public int SortOrder { get; set; }
        //SEO
        [MaxLength(70)]
        public string PageTitle { get; set; }
        [MaxLength(100)]
        public string MetaDescription { get; set; }
        [MaxLength(150)]
        public string MetaKeywords { get; set; }
        [MaxLength(56)] //50 + "-" + 99,000
        public string Slug { get; set; }
    }
}

Data Context

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

namespace CloudOne.Models
{
    public class SiteDataContext: DbContext
    {
        public DbSet<Brand> Brands { get; set; }

        // Twist our database
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            base.OnModelCreating(modelBuilder);
        }
    }
}

The Seed initializer

public class SiteDataContextInitializer: DropCreateDatabaseIfModelChanges<SiteDataContext>
    {
        protected override void Seed(SiteDataContext context)
        {
            var brands = new List<Brand>()
            {
                new Brand { BrandName = "Brand 1", Slug = "brand-1" },
                new Brand { BrandName = "Brand 2", Slug = "brand-2" }
            };

            brands.ForEach(d => context.Brands.Add(d));

            base.Seed(context);
        }
    }

Try to copy and paste this code, then write some code to trigger the database creation (i.e. try to fetch the records and show in the index page).


I have tried and is like EF4 CTP5 is totally ignoring it. What can be wrong?

using section:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Conventions.Edm;

DbContext:

public class SiteDataContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<BlogFeedback> BlogFeedbacks { get; set; }
        public DbSet<BlogCategoryList> BlogCategoryLists { get; set; }
        public DbSet<BlogCategory> BlogCategories { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
            base.OnModelCreating(modelBuilder);
        }
    }

POCO classes

public class Blog
    {...}
public class BlogFeedback
    {...}
public class BlogCategoryList
    {...}
public class BlogCategory
    {...}

Tables generated:

Blogs
BlogCategories
BlogCategoryLists
BlogFeedbacks

What I need:

Blog
BlogCategory
BlogCategoryList
BlogFeedback

One thing may be different is I split my solution into two projects Core and Web. Core has Models, Services and all Code First. Web has only Controllers and Views and a reference to Core. The SetInitializer().Seed() is inside a function in Core, and in Web global.asax the Core.SetInitializer is called, so keep all CTP5 functions inside Core. Database is being recreated ok, data is filled ok, just the convention keeps PLURAL TABLE NAMES, ignoring the modelBuilder override


Comments

  1. Morris

    • 2016/3/2

    I am just taking a crack at entity framework code first. Following their naming convention, we now have to name our tables plural to not have to intervene with the tool. I know the mappings can be over ridden. My question is, after years of following the singular naming convention are we back to using plural names?

  2. Javier

    • 2020/1/28

    Today I will share the default behavior of Entity Framework that assumes that the table name in the database is pluralized. For example, in the Code First approach you made entity (class) named Student and expect the Student Table will be created. But the default table created in the Db will be Students.

  3. Kameron

    • 2019/6/15

    In EF code first however, the generated tables always are plural. My DbSets are pluralized which I believe is where EF is generating the names but I don't want to singularize these names as I believe it is more pratical to have them plural in code.

  4. Hall

    • 2020/11/2

    Entity Framework - Plural and Singular Table names. 13th Mar 2012. By default, the Entity Framework will assume that all of the names of your tables in your database are either pluralised, or in the case of code first, you would like them to be pluralised when created. E.g. you have a table called "Product" and not "Products", or you want your table to be called "Product" and not "Products"

  5. Shane

    • 2017/9/22

    EF Code First automatically pluralizes the table names. Use a [Table] attribute to explicitly map the entity to a table name: [Table("Towary")] public class Towary { // Whatever properties } It looks like there's a way to disable pluralization gobally too, see Entity Framework Code First naming conventions - back to plural table names?.

  6. Jefferson

    • 2020/1/2

    Singular Table Name Convention in Entity Framework Core v2. All development teams should have naming conventions in place for classes, variables, properties, database table names and foreign keys. One of the conventions we here at ClearlyAgile have used for many years is that database table names should be singular.

  7. Maxim

    • 2017/11/3

    Naming Conventions for Entity Framework Core Tables and Columns. By default, EF Core will map to tables and columns named exactly after your .NET classes and properties. For example, mapping a typical Customer class to PostgreSQL will result in SQL such as the following:

Comments are closed.

Recent Posts