代码之家  ›  专栏  ›  技术社区  ›  Bercovici Adrian

基于EF核二级索引的数据获取

  •  0
  • Bercovici Adrian  · 技术社区  · 4 年前

    如果我已经为表定义了一个二级索引(非聚集索引),那么我想了解如何在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 ?
    }
    
    0 回复  |  直到 4 年前
    推荐文章