如何在 ASP .NET MVC EF 中重命名链接 table(多对多现实)的数据库列(代码优先)

How to rename db columns of linking table (many to many realiton) in ASP .NET MVC EF (code first)

我需要创建这个数据库上下文:

public class Contact
{
    public int ID { get; set; }

    public virtual List<Contact> Employers { get; set; }
    public virtual List<Contact> Staff { get; set; }
}

EF 使用列 Contact_ID 和 Contact_ID1 创建 table ContactContac。如何(在何处)重命名此上下文列?

在您的 DbContextOnModelCreating() 方法中:

modelBuilder.Entity<Contact>().HasMany(x => x.Employers).WithMany().Map(x =>
{
    x.ToTable("ContactEmployers");
    x.MapLeftKey("ContactId");
    x.MapRightKey("EmployerId");
});
  • modelBuilder.Entity<Contact>() => 您正在配置的模型
  • HasMany(x => x.Employers) => 表示它是一个带有导航的多对X关系 属性
  • WithMany() => 将其配置为多对多关系,另一侧没有导航 属性
  • Map() => 配置 table 和 ID 的列