Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework table name convention

I'm using EF 6, have two simple POCO class as below:

public class Person
 {
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
 }

public class Company
{
    public int CompanyId { get; set; }
    public string Name { get; set; }
}

and my context

public class Context : DbContext
{
    public Context() : base("name=codefirst")
    {

    }
    public DbSet<Person> People { get; set; }
    public DbSet<Company> Corporation { get; set; }
}

And EF generated tables : dbo.Companies and dbo.People

My question is why one table name is People and other table name is Companies (I know why is pluralized). I mean, One table use the property name, and the other table use the class name ?

Thanks in advance!

enter image description here

like image 840
Richard D Avatar asked Dec 24 '16 23:12

Richard D


1 Answers

Both are mapped by using their class name.
The one that probably confused you is the mapping of the Person class. It is one of the EF pluralization rules, since people is the plural form of person(meaning more than one person), EF mapped it as People. On the other hand, it simply mapped the Company class as Companies.

You can turn off EF pluralization convention by adding this code in the OnModelCreating() method if you don't like it:

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

like image 166
Gimballock Avatar answered Sep 28 '22 17:09

Gimballock