简体   繁体   English

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

[英]In Entity Framework Core 1.0 how do you rename indexes?

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. 到目前为止,我已经允许EF为项目自动生成索引和键名,但是不幸的是,在一种情况下,我开始达到字符数限制。 Basically I have two tables with clustered keys (4 columns) and need to create a lookup table between both of them. 基本上,我有两个带有聚簇键的表(4列),并且需要在两个表之间创建查找表。 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? 最好在Fluent API中,如何在Entity Framework 7中手动命名外键索引,而不是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? 例如,在下面的简单示例中,如何将租户表FK_tbl_Person_tbl_Tenant_TenantID中的FK重命名为FK_Tenant?

在此处输入图片说明

I've used this tutorial to install EF using VS2015: 我已使用本教程通过VS2015安装EF:

EF - Console Application to New Database EF-新数据库的控制台应用程序

Basically: 基本上:

  • Ensure you are targeting .NET Framework 4.5.1 or later. 确保您的目标是.NET Framework 4.5.1或更高版本。
  • Ensure your are using the latest version of the nuget package manager 确保您使用的是最新版本的nuget软件包管理器
  • Install this two packages: 安装这两个软件包:
    • EntityFramework.MicrosoftSqlServer –Pre EntityFramework.MicrosoftSqlServer –预
    • EntityFramework.Commands –Pre EntityFramework.Commands –Pre

I've created a simple DbContext with two entities: 我用两个实体创建了一个简单的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; }
}

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: 然后,您需要使用Package Manager控制台为您的上下文创建迁移:

  • Add-Migration MyFirstMigration 添加迁移MyFirstMigration

And finally update your database using the Package Manager Console: 最后使用Package Manager控制台更新数据库:

  • Update-Database 更新数据库

Using Sql Server Management Studio, I can check the name of my indexes : 使用Sql Server Management Studio,我可以检查索引的名称:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Entity Framework Core 进行全文搜索? - How do you do fulltext search with Entity Framework Core? 如何在Entity Framework Core 1.0 RTM中关闭级联删除 - How to turn off cascade delete in Entity Framework Core 1.0 RTM 如何在.NET Core 1.0中使用Entity Framework运行sql脚本? - How to run sql script with Entity Framework in .NET Core 1.0? 您如何在Entity Framework Core中创建自引用实体? - How do you create a self-referencing entity in Entity Framework Core? 如何使用自定义 class 作为具有 Entity Framework Core 转换的实体的属性? - How do you use a custom class as a property of an entity with a conversion with Entity Framework Core? 实体框架核心1.0连接字符串 - Entity Framework Core 1.0 Connection Strings 您如何使用 Entity Framework Core 进行全文搜索添加两个字段? - How do you do fulltext search add two field with Entity Framework Core? 在 Entity Framework Core 中创建迁移时如何配置 DbContext? - How do you configure the DbContext when creating Migrations in Entity Framework Core? 如何使用实体框架核心1.0从ASP.MVC核心的数据库中读取记录 - How to Read Record From Database in ASP.MVC Core using Entity Framework Core 1.0 如何在Entity Framework Core中正确处理SQL_VARIANT? - How do you properly handle SQL_VARIANT in Entity Framework Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM