9

I've had some experiences in ORM framework such as Hibernate, even Entity Framework 3.0.

By default, those frameworks use singular name for table. For example, class User will map to table User.

But when I migrate to EF 5.x by using Visual Studio 2012, it uses plural name and causes many errors unless I manually map that class by using TableAttribute as:

[Table("User")]
public class User {
    // ...
}

Without TableAttribute, if I have a DbContext as below:

public CustomContext : DbContext {
    // ...
    public DbSet<User> Users { get; set; }
}

Then calling:

var list = db.Users.ToList();

The generated sql looks like:

Select [Extent1].[Username] From [Users] as [Extent1]

Therefore, the error will occur because I don't have any table named Users. Futhermore, I refer to name tables in singular form to plural form. You can see why in this link

I wonder why Microsoft implement EF 5.x that way rather than same to EF 3.0?

Updated:

We can tell EF not use pluralized name by using this code in the context class:

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

    base.OnModelCreating(modelBuilder);
}

Even if EF tool generates pluralized name, it should keep the mappings to the original database, shouldn't it? Because some users might want to change a few name of fields on their class. EF 3.0 works well, but EF 5.x does not.

Guys, can you give my a reason, please!

2 Answers 2

12

That convention is defined in the PluralizingTableNameConvention convention defined by default in the DbModelBuilder.Conventions

If you want to exclude it from all tables, you can use the code from this question.

Why this is done the way it is, I do not know, but I must say that I am, personally, in the camp that thinks that table names should be pluralized :)

Of the example databases provided with an SQL Server instance, Northwind has plurals, and AdventureWorks uses a singular form, so at best, there is no established standard. I've had quite a few discussion for or against each one, but one thing that everybody can agree on is that when once pick a naming strategy, you should stick with it.

4
  • Thanks! Yet, it's known that in 2 Microsoft's database samples (Northwind and AdventureWorks), the table names in singularized. Do you agree with me?
    – Tu Tran
    Dec 20, 2012 at 10:07
  • I totally agree with your appended answer. Despite table is pluralize, EF should keep the mapping even we rename the ORM class, shouldn't it?
    – Tu Tran
    Dec 20, 2012 at 10:22
  • If you are unsure, or if you might want to change the ORM class name, then I think it's best to be explicit, and use a TableAttribute or a ToTable() call of the fluent API
    – SWeko
    Dec 20, 2012 at 10:32
  • Yes, I did. But I confused because those classes have been generated by EF Tool inself.
    – Tu Tran
    Dec 20, 2012 at 11:23
0

When you generate your model with entity framework, uncheck "Pluralize or singularize generated object names"

1
  • 1
    No, because that is a set of object, it's ok to pluralize the name. But it does not mean that the generated sql should be pluralized.
    – Tu Tran
    Dec 20, 2012 at 9:55

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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