我有这个实体:
# get the unique weeks in the headers
weeks <- names(eidsr)[grepl('W[[:digit:]]_[[:digit:]]{4}', names(eidsr))] %>%
substr(1, 7) %>%
unique()
# apply the function on all the weeks, bind them with the district, and convert to data.frame
cbind('district' = eidsr$district, sapply(weeks, wacky_fun)) %>%
as.data.frame()
我看到[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");
});
行被删除?怎么了?
注意:我看不到来自另一个表中两个字段的复合键的此问题。
答案 0 :(得分:3)
主键由索引强制执行。
StudentId
是该索引的第一列。
它不需要在其上定义另一个索引(这样的索引几乎肯定也毫无意义,因为PK索引很可能是表的聚集索引,而非聚集索引在其叶子处包含PK列)
答案 1 :(得分:1)
StudentId
上的附加索引将是多余的。由于这是主键中的第一列,因此在StudentId
上的查找可以仅使用其索引。同样,在StudentId
+ EmployerId
上进行查找也可以做到这一点。
有趣的是,EF Core曾经不是,并且创建了两个索引。现在,删除索引这一事实意味着此行为已得到修复/优化。