When working with Entity Framework, you often need to know the entity set name (the pluralized form) for a given entity type. Entity Framework uses pluralization conventions to map between singular entity class names and plural entity set and table names. This guide explains how pluralization works, how to get the entity set name programmatically, and how to customize naming conventions.
Understanding Entity Framework Pluralization
Entity Framework follows a convention where:
- Entity classes use singular names:
Customer,Order,Product - Entity sets (collections) use plural names:
Customers,Orders,Products - Database tables use plural names by default:
Customers,Orders,Products
This convention applies in both Database-First and Code-First approaches, though the specific behavior differs.
Getting the Entity Set Name at Runtime
Method 1: Using EntityKey.EntitySetName (EF4/EF6)
The most reliable way to get the entity set name for an entity that is already tracked by the context:
// For an entity that has an EntityKey (ObjectContext)
string entitySetName = myEntity.EntityKey.EntitySetName;
// Returns "Customers" for a Customer entity
This approach works because the EntityKey contains the actual entity set name as configured in the model, regardless of any custom naming conventions.
Method 2: Querying the ObjectContext Metadata (EF4/EF6)
For ObjectContext-based models, you can query the metadata workspace:
public string GetEntitySetName<T>(ObjectContext context)
{
var container = context.MetadataWorkspace
.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);
var entitySetName = container.BaseEntitySets
.Where(es => es.ElementType.Name == typeof(T).Name)
.Select(es => es.Name)
.FirstOrDefault();
return entitySetName;
}
// Usage
string setName = GetEntitySetName<Customer>(objectContext);
// Returns "Customers"
Method 3: Using DbContext (EF6)
With DbContext, you can access the entity set name through the underlying ObjectContext:
public string GetEntitySetName<T>(DbContext dbContext) where T : class
{
var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
var entityType = typeof(T);
var container = objectContext.MetadataWorkspace
.GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace);
var entitySet = container.BaseEntitySets
.FirstOrDefault(es => es.ElementType.Name == entityType.Name);
return entitySet?.Name;
}
Method 4: Using EF Core Metadata
In Entity Framework Core, the approach is different:
public string GetTableName<T>(DbContext context) where T : class
{
var entityType = context.Model.FindEntityType(typeof(T));
return entityType?.GetTableName();
}
// Usage
using var context = new MyDbContext();
string tableName = GetTableName<Customer>(context);
// Returns "Customers" (or whatever the table is mapped to)
The PluralizationService
Entity Framework includes a PluralizationService class that handles the English-language rules for converting between singular and plural forms.
Using PluralizationService Directly
using System.Data.Entity.Design.PluralizationServices;
using System.Globalization;
var pluralizer = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-US"));
// Pluralize
string plural = pluralizer.Pluralize("Customer"); // "Customers"
string plural2 = pluralizer.Pluralize("Person"); // "People"
string plural3 = pluralizer.Pluralize("Category"); // "Categories"
string plural4 = pluralizer.Pluralize("Mouse"); // "Mice"
string plural5 = pluralizer.Pluralize("Status"); // "Statuses"
// Singularize
string singular = pluralizer.Singularize("Customers"); // "Customer"
string singular2 = pluralizer.Singularize("People"); // "Person"
// Check if already plural/singular
bool isPlural = pluralizer.IsPlural("Customers"); // true
bool isSingular = pluralizer.IsSingular("Customer"); // true
Limitations
- Only supports English. Other languages are not supported.
- Some irregular words may not pluralize correctly.
- The result may not match your actual entity set name if you have used custom naming.
Customizing Naming Conventions
Disabling Pluralization in EF6 Code-First
public class MyDbContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Remove the pluralizing convention
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
With this convention removed, the Customer entity maps to a Customer table instead of Customers.
Custom Table Names in EF6
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().ToTable("tbl_Customer");
modelBuilder.Entity<Order>().ToTable("tbl_Order");
}
Disabling Pluralization in EF Core
EF Core does not pluralize table names by default in later versions. If you need to control table naming:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Explicit table name for a specific entity
modelBuilder.Entity<Customer>().ToTable("Customer");
// Apply a naming convention to all entities
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
entityType.SetTableName(entityType.DisplayName());
}
}
Custom Pluralization in EF Core
You can implement a custom pluralization service by creating a class that implements IPluralizer:
public class CustomPluralizer : IPluralizer
{
public string Pluralize(string name)
{
// Custom logic
if (name.EndsWith("Status"))
return name + "es";
if (name.EndsWith("y") && !name.EndsWith("ey"))
return name.Substring(0, name.Length - 1) + "ies";
return name + "s";
}
public string Singularize(string name)
{
if (name.EndsWith("ies"))
return name.Substring(0, name.Length - 3) + "y";
if (name.EndsWith("ses"))
return name.Substring(0, name.Length - 2);
if (name.EndsWith("s"))
return name.Substring(0, name.Length - 1);
return name;
}
}
Register it in your design-time services:
public class MyDesignTimeServices : IDesignTimeServices
{
public void ConfigureDesignTimeServices(IServiceCollection services)
{
services.AddSingleton<IPluralizer, CustomPluralizer>();
}
}
Table Naming Strategies
Different teams use different naming conventions for database tables. Here are common approaches:
| Strategy | Entity Name | Table Name | Example |
|---|---|---|---|
| Pluralized (default) | Customer | Customers | EF default |
| Singular | Customer | Customer | Common in DBA-managed databases |
| Prefixed | Customer | tbl_Customer | Legacy convention |
| Schema-qualified | Customer | sales.Customer | Using database schemas |
| Snake case | CustomerOrder | customer_order | Common in PostgreSQL |
Resumen
To get the plural form (entity set name) of an entity type in Entity Framework, the most reliable approach is to use EntityKey.EntitySetName for tracked entities or to query the metadata workspace. The PluralizationService class can calculate the English plural form of a word, but it may not match your actual entity set configuration if custom naming is used. In modern EF Core applications, you can control table naming through the ToTable() method, custom conventions, or a custom IPluralizer implementation. Choose the naming convention that aligns with your team’s database standards and configure Entity Framework accordingly.