Skip to content

EF Core: Table names now taken from DbSet names (starting in RC2) #167

Open
@rowanmiller

Description

@rowanmiller

In past pre-release of EF Core, the table name for an entity was the same as the entity class name. In RC2 we now use the name of the DbSet property. If no DbSet property is defined for the given entity type, then the entity class name is used.

For example, given the following model, in RC1 the table name for Blog would have been dbo.Blog, in RC2 it will be dbo.Blogs.

public class Blogging : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    ...
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

Impact on existing apps

If you upgrade an app from RC1 to RC2, you have several options:

  • If you are generating the database from the model, you can scaffold a new migration to apply the naming changes (or drop and recreate the database if you are using EnsureCreated() rather than migrations).
  • Manually specify table names to match what was chosen in RC1. This can be done in OnModelCreating or with the [Table] attribute. See the docs for more info.
  • Use the code from the following section to bulk configure table names to match what was chosen in RC1.

Reverting to RC1 naming

If you want to revert back to the RC1 naming conventions for tables, add the following code to the start of the OnModelCreating method on your context (requires a using statement for Microsoft.EntityFrameworkCore.Metadata.Internal).

foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
    entity.Relational().TableName = entity.DisplayName();
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions