Home > Blockchain >  In .NET EF database-first, is HasIndex() required?
In .NET EF database-first, is HasIndex() required?

Time:12-29

I used Scaffold-DbContext to create an EF Core data model from an SQL Server database, and then went ahead and added many custom attribute decorations and [NotMapped] columns to the model classes, as well as some changes to OnModelCreating.

Now I realized that I need to add indices to the foreign keys (was under the impression that they are added automatically). So I'm doing that and its a breeze with DbSchema, but here is my question: originally, Scaffold-DbContext added an

entity.HasIndex(e => e.[column name], "[index name");

call for each existing index.

Is this important to add for every new index I create? Do I need to use the actual index name?

I'm using .NET 6 and EF Core v6.0.8 .

Thanks!

CodePudding user response:

No you do not need those, only if you start creating migrations based on the scaffolded DbContext (not recommended).

EF Core does not make direct use of the indexes, your RDBMS takes care of that.

Consider having a look at EF Core Power Tools, and avoid changes to the already generated files (use partial classes and other extension points)

  • Related