I have a .Net core 3.1 API with EF core code first migrations enabled where I am trying to add a clustered index on a non PK column.
I have a Vehicle entity:
public class Vehicle
{
// PK and has clustered index by default
[Key]
public int Id { get; set; }
// FK and needs to be clustered
public long CompanyId { get; set; }
[ForeignKey(nameof(CompanyId))]
public virtual Company Company { get; set; }
}
I created a code first migration for the above model, then updated the database.
At this point the table has PK as Id
with clustered index and CompanyId
just as FK.
To remove clustered index from Id
and to add Clustered Index on CompanyId
I wrote the following in Fluent API in the OnModelCreating()
method
// Remove ClusteredIndex from PK
modelBuilder.Entity<Vehicle>().HasIndex(m => m.Id).IsUnique(true).IsClustered(false);
// Add clustered index on CompanyId
modelBuilder.Entity<Vehicle>().HasIndex(m => m.CompanyId).IsUnique(false).IsClustered(true);
When I run the API, I can see that the above ccode does execute but it has no effect on the SQL DB and does not change the indexing as desired.
What am I missing here? How do I add a clustered index on CompanyId
column?