首页 > 解决方案 > 根据 EF Core 中的二级索引获取数据

问题描述

如果我为我的表定义了二级索引(非聚集),我想了解如何在 EF Core 中有效地检索数据。

我希望能够基于二级索引对单个元素进行有效查找:

public class Entity
{
   [Key]
   public long Id{get;set;}
   [Index(IsClustered=false,IsUnique=false)]
   public string SecondaryId{get;set;}
}

public class MyContext:DbContext{
   public DbSet<Entity> Entities{get;set;}
}

public void Find(long primaryIndex,string SecondaryId,MyContext context)
{
   var normalSearch=context.Entities.Find(primaryIndex);
   var secondaryIndexSearch=context.Entites.Find(SecondaryId); //does it know to look at the `SecondaryId` column and detect that its a secondary index ?
}

标签: sql-serveref-core-3.1secondary-indexes

解决方案


推荐阅读