我试图与客户一起在许可证表中创建复合键和外键,但是当我运行EF命令时update-database,会在许可证表中生成以下列。
我有以下表格
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
public ICollection<License> Licenses{ get; set; }
}
public class License
{
public int Id { get; set; }
public int Count { get; set; }
public int CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
public class MyDbContext : DbContext
{
public MyDbContext() : base("TestConnection")
{
}
public DbSet<Company> Company { get; set; }
public DbSet<Customer> Customer { get; set; }
public DbSet<License> License { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>()
.HasKey(cust => new { cust.Id, cust.CompanyId })
.Property(cust => cust.Id).HasColumnOrder(1)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Customer>()
.Property(cust => cust.CompanyId).HasColumnOrder(2);
modelBuilder.Entity<License>()
.HasKey(lic => new { lic.Id, lic.CustomerId })
.Property(lic => lic.Id).HasColumnOrder(1)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<License>()
.Property(lic => lic.CustomerId).HasColumnOrder(2);
base.OnModelCreating(modelBuilder);
}
}
注意:我希望 CustomerId 在 License 表中充当 PK 和 FK,但不知道那些名为Customer_Id和 Customer_CompanyId` 的额外列是如何生成的。
