简体   繁体   中英

Create INDEX with relational index options using Entity Framework Core

I have an index like this:

CREATE NONCLUSTERED INDEX [CIX_MyIndex]
ON [dbo].[TableWithData]([ColumnToAddIndex])
WITH (STATISTICS_NORECOMPUTE = OFF, DROP_EXISTING = OFF, ONLINE = ON) 
ON [PRIMARY]

And with this code, I can create the index due to STATISTICS_NORECOMPUTE = OFF, DROP_EXISTING = OFF are the defaults values

entity.HasIndex(e => e.ColumnToAddIndex)
      .HasName("CIX_MyIndex")
      .IsCreatedOnline();

But if I want to change these values to ON, I don't find any way to do that, is it possible to use Entity Framework Core to configure indexes like Transact SQL and use any of the relational index options documented in this link?

https://learn.microsoft.com/en-us/sql/t-sql/statements/create-index-transact-sql?view=sql-server-ver16

I tried to use the HasAnnotation method, but I don't know how to configure the string that is required.

entity.HasIndex(e => e.ColumnToAddIndex)
      .HasName("CIX_MyIndex")
      .HasAnnoation("STATISTICS_NORECOMPUTE", true)
      .IsCreatedOnline();

Looks like.HasFilter doesn't work, In which case it doesn't seem possible to use EF core to configure this, EF core provides a limited set of options for configuring indexes when compared to the options available in TSQL.

I think the only option is to use the RawSQL

context.Database.ExecuteSqlCommand("CREATE NONCLUSTERED INDEX [CIX_MyIndex] ON [dbo].[TableWithData]([ColumnToAddIndex]) WITH (STATISTICS_NORECOMPUTE = ON, DROP_EXISTING = ON, ONLINE = ON) ON [PRIMARY]");

https://learn.microsoft.com/en-us/ef/core/modeling/indexes?tabs=data-annotations#index-filter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM