c# - EF Core - 复合主键删除一个索引
问题描述
我有这个实体:
[Table("order")]
public class Order
{
[Key]
[Column("id")]
public Guid Id { get; set; }
[MaxLength(128)]
[Column("description")]
public string Description { get; set; }
[ForeignKey("Student")]
[Column("student_id")]
public Guid StudentId { get; set; }
[ForeignKey("Employer")]
[Column("employer_id")]
public Guid EmployerId { get; set; }
[ForeignKey("Customer")]
[Column("customer_id")]
public Guid CustomerId { get; set; }
public virtual Guid Student { get; set; }
public virtual Guid Employer { get; set; }
public virtual Guid Customer { get; set; }
}
我看到了下一个快照DbContext
:
modelBuilder.Entity("DataInterface.Models.Order", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnName("id");
b.Property<string>("Description")
.HasColumnName("description")
.HasMaxLength(128);
b.Property<Guid>("StudentId")
.HasColumnName("student_id");
b.Property<Guid>("EmployerId")
.HasColumnName("employer_id");
b.Property<Guid>("CustomerId")
.HasColumnName("customer_id");
b.HasKey("Id");
b.HasIndex("StudentId");
b.HasIndex("EmployerId");
b.HasIndex("CustomerId");
b.ToTable("order");
});
你怎么能看到 - 一切都很好。但我需要创建一个复合键并删除主键。所以,让我们去做吧。我的实体新代码(没有Id
主键):
[Table("order")]
public class Order
{
[MaxLength(128)]
[Column("description")]
public string Description { get; set; }
[ForeignKey("Student")]
[Column("student_id")]
public Guid StudentId { get; set; }
[ForeignKey("Employer")]
[Column("employer_id")]
public Guid EmployerId { get; set; }
[ForeignKey("Customer")]
[Column("customer_id")]
public Guid CustomerId { get; set; }
public virtual Guid Student { get; set; }
public virtual Guid Employer { get; set; }
public virtual Guid Customer { get; set; }
}
OnModelCreating
我在上下文的方法中添加了新行为:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>()
.HasKey(x => new { x.StudentId, x.EmployerId, x.CustomerId });
}
当我添加新迁移时,我看到索引已StudentId
被删除。我不明白为什么?我的新快照DbContext
:
modelBuilder.Entity("DataInterface.Models.Order", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnName("id");
b.Property<string>("Description")
.HasColumnName("description")
.HasMaxLength(128);
b.Property<Guid>("StudentId")
.HasColumnName("student_id");
b.Property<Guid>("EmployerId")
.HasColumnName("employer_id");
b.Property<Guid>("CustomerId")
.HasColumnName("customer_id");
b.HasKey("StudentId", "EmployerId", "CustomerId");
b.HasIndex("EmployerId");
b.HasIndex("CustomerId");
b.ToTable("order");
});
为什么删除了三个索引之一?为什么b.HasIndex("StudentId");
行被删除?怎么了?
注意:我没有看到来自另一个表中两个字段的复合键的这个问题。
解决方案
主键由索引强制执行。
StudentId
是该索引中的第一列。
它不需要在其上定义另一个索引(这样的索引几乎肯定也毫无意义,因为 PK 索引很可能是表的聚集索引,而非聚集索引在其叶子中包含 PK 列)
推荐阅读
- matlab - fsolve matlab中的多项式不起作用
- django - Django模型两步过滤器一个是用户,另一个是客户
- vue.js - 命名空间模块身份验证的重复命名空间身份验证/
- django - Django:添加2个变量并在同一页面上获得响应
- react-native - react-native 如何访问所有页面中的侧边菜单?
- tsql - 如何在 Antlr4 语法中实现 EOF
- java - 对 java 进程的 Ansible 检查开始执行
- php - 如何从数据库中获取 Json 输出中的特定列
- angular - 如何使用 Ngrx 和 Reactive Form 在 Angular 中显示日期和时间戳
- python - Python:字符串操作难度