How to create a Clustered Index with Entity Framework Core

前端 未结 4 627
广开言路
广开言路 2020-12-10 12:53

From EF6.1, we have a way of specifying a clustered index on a property

public class Person 
{
  [Index(IsClustered = true, IsUnique = true)]
  public long U         


        
4条回答
  •  感动是毒
    2020-12-10 13:37

    For EF Core 3.0+ You can now use IsClustered:

    modelBuilder.Entity()
    .HasIndex(e => e.UserName)
    .IsUnique()
    .IsClustered();
    

    .ForSqlServerIsClustered() is now marked as obsolete.

    Also be aware that if you have a Primary Key on the table you may also need to explicitly remove the clustering on it before you add the clustering on your Username:

    modelBuilder.Entity()
    .HasKey(e => e.PersonId)
    .IsClustered(false);
    
    modelBuilder.Entity()
    .HasIndex(e => e.UserName)
    .IsUnique()
    .IsClustered();
    

提交回复
热议问题