Take the 2-minute tour ×

I'm using Entity Framework Code First. Usually I have no problem, but for a database I'm working with I keep getting errors that it can't find the table in the SQL Server database. Here is what my class looks like:

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

public class CustomApp_User
    public int UserID { get; set; }
    public string Username { get; set; }
    public string Email { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateModified { get; set; }


In the database I have a table called "CustomApp_Users" to match the above. Note it has the "s" at the end.

And then I have:

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

public class CustomAppDB : DbContext
    public DbSet<CustomApp_User> CustomApp_Users { get; set; }

I expected EF codefirst to pluralize so that it would find "CustomApp_Users" in the database since this is how it usually works. But instead I get the error:

Invalid object name 'dbo.CustomApp_User'.

It appears it's not pluralizing the table name. I can't figure out why. One thing different with this database is that the Primary Keys do not follow the normal convention so I use the [Key] annotation.

I do know that if I use the [Table] annotation for my class it will work: [Table("CustomApp_Users")]

But, I'd like to find out why the pluralization is not working the way I thought it would.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

That is because the PluralizationService in EF can not pluralize it. It returns the same string if you pass it "CustomApp_User". Unfortunately you can not customize this service. So you need to configure the table name explicitly.

share|improve this answer
Thank you. I didn't know we could call the PluralizationService. It seems if the table name has a an underscore in it then it won't pluralize it. If I take out the underscore it pluralizes. I wonder why they did that? –  dtc Dec 20 '11 at 3:17
@metanaito Maybe because it is not a good practice to use _ marks in class names. –  Eranga Dec 20 '11 at 6:53
I have never heard that before. I'm wondering then, perhaps I should change my "CustomApp_User" naming to "CustomAppUser" just to make everything consistent and work together. That would solve my pluralization issue as well, though I kind of like explicitly defining the table name. Not too sure if this naming will cause confusion down the line somewhere. –  dtc Dec 20 '11 at 18:46
add comment

Your Answer


