dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.62k stars 1.96k forks source link

Clarify how EF uses indexes (i.e. currently only for update ordering) #1677

Open smcga opened 5 years ago

smcga commented 5 years ago

I understand that when using migrations, indexes defined in OnModelCreating will be created in the database.

I have an existing database, with indexes already set up, and was wondering if there is a benefit to defining these indexes in my DbContext? Does EFCore use these index definitions to influence how it builds queries, or does defining an existing index have no effect?

Thanks


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

smcga commented 5 years ago

I found an answer for this on SO - https://stackoverflow.com/questions/54299018/what-is-the-benefit-of-adding-hasindex-in-your-mappings-on-a-dbfirst-scenari

tl;dr - If you are not updating the DB with migrations, this does nothing.

offirpeer commented 4 years ago

If I created an index in my DB, how can I tell EF to use it when fetching the data? https://stackoverflow.com/questions/62881671/ef-core-not-fetching-data-from-table-based-on-its-clustered-index