In Entity Framework Core 1.0 how do you rename indexes?

c# entity-framework entity-framework-core

Question

Up to this point I've allowed EF to auto generate index and key names for my project but unfortunately im starting to hit the character limit in one scenario. Basically I have two tables with clustered keys (4 columns) and need to create a lookup table between both of them. Once I do that tho, I get the error:

"The identifier that starts with [Long FK Name Here] is too long. Maximum length is 128.

Preferably in Fluent API, how can I manually name a foreign key index in Entity Framework 7 so its not FK_table2_table1_table1ID? For instance in my simple example below how would I rename the FK from the Tenant Table FK_tbl_Person_tbl_Tenant_TenantID to FK_Tenant?

enter image description here

Popular Answer

I've used this tutorial to install EF using VS2015:

EF - Console Application to New Database

Basically:

  • Ensure you are targeting .NET Framework 4.5.1 or later.
  • Ensure your are using the latest version of the nuget package manager
  • Install this two packages:
    • EntityFramework.MicrosoftSqlServer –Pre
    • EntityFramework.Commands –Pre

I've created a simple DbContext with two entities:

using Microsoft.Data.Entity;
using System.Collections.Generic;
public class SampleContext : DbContext
{
    public DbSet<Person> People { get; set; }

    public DbSet<Tenant> Tenants { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // Visual Studio 2015 | Use the LocalDb 12 instance created by Visual Studio
        optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;");

        // Visual Studio 2013 | Use the LocalDb 11 instance created by Visual Studio
        // optionsBuilder.UseSqlServer(@"Server=(localdb)\v11.0;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // Configure the name of the foreign key
        modelBuilder.Entity<Person>()
            .HasOne(p => p.Tenant)
            .WithMany(t => t.Persons)
            .HasConstraintName("MyForeignKey");

        // Configure the name of a unique index
        modelBuilder.Entity<Person>().HasAlternateKey(p => p.Email).ForSqlServerHasName("MyUniqueEmail");

    }
}

public class Person
{
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public virtual Tenant Tenant { get; set; }
}

public class Tenant
{
    public Tenant()
    {
        Persons = new HashSet<Person>();
    }

    public int TenantId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Person> Persons { get; set; }
}

So to configure the name of the foreign key:

modelBuilder.Entity<Person>()
            .HasOne(p => p.Tenant)
            .WithMany(t => t.Persons)
            .HasConstraintName("MyForeignKey");

To configure the name of a unique index:

modelBuilder.Entity<Person>().HasAlternateKey(p => p.Email).ForSqlServerHasName("MyUniqueEmail");

Then you need to create a migration for your context using the Package Manager Console:

  • Add-Migration MyFirstMigration

And finally update your database using the Package Manager Console:

  • Update-Database

Using Sql Server Management Studio, I can check the name of my indexes :

enter image description here



Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why