在Entity Framework Core 1.0中,如何重命名索引?

c# entity-framework entity-framework-core

到目前為止,我已經允許EF為我的項目自動生成索引和鍵名,但不幸的是我開始在一個場景中達到字符限制。基本上我有兩個帶有聚簇鍵(4列)的表,需要在它們之間創建一個查找表。一旦我這樣做,我得到錯誤:

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

最好在Fluent API中,如何在Entity Framework 7中手動命名外鍵索引,使其不是FK_table2_table1_table1ID?例如,在下面的簡單示例中,如何將FK從租戶表FK_tbl_Person_tbl_Tenant_TenantID重命名為FK_Tenant?

在此處輸入圖像描述

熱門答案

我使用本教程使用VS2015安裝EF:

EF - 控制台應用程序到新數據庫

基本上:

  • 確保您的目標是.NET Framework 4.5.1或更高版本。
  • 確保您使用的是最新版本的nuget包管理器
  • 安裝這兩個包:
    • EntityFramework.MicrosoftSqlServer - Pre
    • EntityFramework.Commands - Pre

我用兩個實體創建了一個簡單的DbContext:

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; }
}

所以要配置外鍵的名稱:

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; }
}

要配置唯一索引的名稱:

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; }
}

然後,您需要使用程序包管理器控制台為您的上下文創建遷移:

  • 添加遷移MyFirstMigration

最後使用Package Manager控制台更新數據庫:

  • 更新數據庫

使用Sql Server Management Studio,我可以檢查索引的名稱:

在此處輸入圖像描述




許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因