prb
prb

Reputation: 189

EF Core 2.0 migrations naming conventions

I am trying to move a project that was using EF 6 to EF Core 2.

The migrations in the old project produced a database with a "dbo." prefix on everything, e.g. tables are "dbo.Something", foreign keys are "FK_dbo.Something_dbo.SomethingElse_Id".

But EF Core doesn't follow this convention, it leaves out the "dbo.".

Is there a way to get EF Core to follow the old convention so my existing databases will still work?

Upvotes: 2

Views: 1426

Answers (2)

Fati Iseni
Fati Iseni

Reputation: 451

There is no straightforward way to achieve this. The only way is to construct the PK and FK names manually. Override the OnModelCreating method of DBContext as below. If you already have definitions within OnModelCreating, then make sure to paste the code in the end (after your fluent api configurations, if any).

    protected override void OnModelCreating(ModelBuilder builder)
    {
        foreach (var pk in builder.Model
                    .GetEntityTypes()
                    .SelectMany(t => t.GetKeys()))
        {
            pk.Relational().Name = "PK_dbo." + pk.DeclaringEntityType.ClrType.Name;
        }

        foreach (var fk in builder.Model
                .GetEntityTypes()
                .SelectMany(t => t.GetForeignKeys()))
        {
            fk.Relational().Name = "FK_dbo." + fk.DeclaringEntityType.ClrType.Name +
                                     "_dbo." + fk.PrincipalEntityType.ClrType.Name +
                                         "_" + fk.Properties.First().Name;
        }
    }

Upvotes: 0

meziantou
meziantou

Reputation: 21337

You can change the naming convention using the OnModelCreating method of the DbContext. Here's an example for a blog post I wrote:

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // Singularize table name
        // Blogs => Blog
        foreach (var entityType in modelBuilder.Model.GetEntityTypes())
        {
            // Add NuGet package "Humanizer" to use Singularize()
            entityType.Relational().TableName = entityType.Relational().TableName.Singularize();
        }

        // Prefix column names with table name
        // Id => Blog_Id
        foreach (var entityType in modelBuilder.Model.GetEntityTypes())
        {
            foreach (var property in entityType.GetProperties())
            {
                property.Relational().ColumnName = entityType.Relational().TableName + "_" + property.Relational().ColumnName;
            }
        }

        // Rename Foreign Key
        // FK_Post_Blog_BlogId => FK_Post_Blog_BlogId_Test
        foreach (var entityType in modelBuilder.Model.GetEntityTypes())
        {
            foreach (var property in entityType.GetProperties())
            {
                foreach (var fk in entityType.FindForeignKeys(property))
                {
                    fk.Relational().Name = fk.Relational().Name + "_Test";
                }
            }
        }

        // Rename Indices
        // IX_Blog_Url => IX_Blog_Url_Test
        foreach (var entityType in modelBuilder.Model.GetEntityTypes())
        {
            foreach (var index in entityType.GetIndexes())
            {
                index.Relational().Name = index.Relational().Name + "_Test";
            }
        }
    }
}

Upvotes: 1

Related Questions