7

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

1
  • Both use the type name, only they're pluralized. The plural of Person is People
    – haim770
    Dec 26, 2016 at 10:35

2 Answers 2

7

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>();

2
  • OP knows that EF does pluralization while creating tables and he doesn't want to stop it from happening.
    – RBT
    Dec 25, 2016 at 2:58
  • @RBT Yes, but he was expecting Person class to be mapped as Persons. I explained that it won't be.
    – Gimballock
    Dec 25, 2016 at 12:11
2

Entity Framework (EF) model builder always refers to your class names while creating tables in database in code first approach. In your case it is Person and Company. EF never refers to properties in your context class while creating database tables.

While creating the table, it tries to pluralize the names so Person got pluralized to People and Company got pluralized to Companies.

Just as a quick test if you change the name of your Company class to Corporation then the table name will get created as Corporations. I've also changed the corresponding property name Corporation in context class to FooBar

Here is what I did to test it:

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

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

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

EF now goes ahead and creates Corporations table instead (plural of Corporation) and ignores the FooBar property name altogether which is present in the context class .

In your code it was a mere coincidence that you had named the property in your context class as People which happen to be plural of Person. This confused you and got you thinking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.