[英]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: 基本上:
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控制台为您的上下文创建迁移:
And finally update your database using the Package Manager Console: 最后使用Package Manager控制台更新数据库:
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.