indexing - 为什么在 EF Core 中默认只为复合 PK 定义一个索引?
问题描述
我在实体框架核心(版本=3.1.5)中有这个模型。它表示节点和它们之间的链接,其中每个链接都有一个Parent
节点和一个Children
节点,每个节点有0-n个Children
节点和0-n个Parent
节点。
public class GraphContext : DbContext
{
public DbSet<Node> Nodes { get; set; }
public DbSet<Link> Links { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer("cnxStr");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Node>(n =>
{
n.Property(s => s.Id).ValueGeneratedOnAdd();
});
modelBuilder.Entity<Link>(l =>
{
l.HasKey(t => new { t.ParentId, t.ChildId });
l.HasOne(ls => ls.Parent).WithMany(p => p.Children).HasForeignKey(ls => ls.ParentId ).OnDelete(DeleteBehavior.Restrict);
l.HasOne(ls => ls.Child).WithMany(c => c.Parents).HasForeignKey(ls => ls.ChildId ).OnDelete(DeleteBehavior.Restrict);
// l.HasIndex(d => d.ParentId);
});
}
}
public class Node
{
public Guid Id { get; set; }
public List<Link> Parents { get; set; }
public List<Link> Children { get; set; }
}
public class Link
{
public Guid ParentId { get; set; }
public Node Parent { get; set; }
public Guid ChildId { get; set; }
public Node Child { get; set; }
}
当我创建迁移时,默认包含以下索引:
migrationBuilder.CreateIndex(
name: "IX_Links_ChildId",
table: "Links",
column: "ChildId");
但是,默认情况下不会为Link.ParentId
. 为什么是一个而不是另一个?
备注:我可以添加l.HasIndex(d => d.ParentId);
到模型配置中以使第二个索引出现。
解决方案
推荐阅读
- java - 为什么mybatis更新和删除不起作用?
- java - JFrame:如何添加一个将新文本字段添加到框架的按钮?
- apache-spark - 访问数据框的空指针异常
- c# - 通过依赖于其他属性的属性更新 DataGrid 复选框状态
- sql - 如何将存储过程的结果集存储到表中,其中 SP 的结果集在 json 路径中?
- mongodb - 无法对 mongodb 中已填充的集合进行分片
- list - 点击列表项的空白处没有任何反应,为什么?
- excel - 用户表单组合框大小 - VBA
- r - 如何使用最小和最大列收缩数据集?我可以使用 apply 系列函数吗?
- postgresql - 错误:致命:用户“Django_test”的密码验证失败