Using Singular Table Names with EF Core 2
You can use exactly the same code. Relational()
is extension method defined in the RelationalMetadataExtensions
class inside Microsoft.EntityFrameworkCore.Relational.dll
assembly, so make sure you are referencing it.
What about IPluralizer
, as you can see from the link it's just a Pluralization hook for DbContext Scaffolding, i.e. entity class generation from database, used to singularize entity type names and pluralize DbSet names. It's not used for table name generation. The default table name convention is explained in Table Mapping section of the documentation:
By convention, each entity will be setup to map to a table with the same name as the
DbSet<TEntity>
property that exposes the entity on the derived context. If noDbSet<TEntity>
is included for the given entity, the class name is used.
You can do it this way without using internal EF API calls by using the ClrType.Name
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
// Use the entity name instead of the Context.DbSet<T> name
// refs https://docs.microsoft.com/en-us/ef/core/modeling/entity-types?tabs=fluent-api#table-name
modelBuilder.Entity(entityType.ClrType).ToTable(entityType.ClrType.Name);
}
}