Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rowanmiller opened this issue Apr 5, 2016 · 1 comment
Open

Comments

@rowanmiller
Copy link

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();
}
@rowanmiller rowanmiller added this to the 1.0.0-rc2 milestone Apr 5, 2016
@aspnet aspnet locked and limited conversation to collaborators Apr 5, 2016
@divega
Copy link

divega commented Apr 6, 2016

You can use dotnet/efcore#4996 for discussions.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants