1

I have a table in SQL Server which includes custom nonclustered indexes for some columns. And I have also a project with below versions.

  • .Net Framework 4.6.2
  • Entity Framework 6.4.4

I added an entity data model to project based on my existing database (using a database-first approach).

Although a SELECT query returns quickly in database, the same query gets a timeout error in the project. And I thought that EF couldn't add column indexes to the project. I searched index names in the project, but there isn't any code about indexes in the project.

So, how can I add indexes to my database-first model?

5
  • 1
    The indexes aren't added to EF - if anything, they would be added in the SQL Server database..... From EF, you'd have to use migrations to do something like this (change/modify the structure of the database) Commented Nov 29, 2021 at 9:10
  • You could just add a migration and execute arbitrary SQL in the migration, including a "create index...". Commented Nov 29, 2021 at 9:19
  • 1
    It's really unclear what you're asking, mainly because you seem to have a wrong perception of what indexes are and where they play a role. Stick to the problem: differences in performance. Are you executing exactly the same SQL query as EF does? Commented Nov 29, 2021 at 9:37
  • Database first means you create the database first, including any indexes. there isn't any code about indexes in the project. there wouldn't be. You don't write any index-specific code in your SQL query after all. Commented Nov 29, 2021 at 14:13
  • the same query gets a timeout error in the project what does your code do? How much data is there? Commented Nov 29, 2021 at 14:16

1 Answer 1

1

So, how can I add indexes to my database-first model?

"database-first" means that you apply design changes to the database first, and then apply any changes to the EF model second.

So you just create the indexes in SQL Server using Visual Studio or SSMS, and since EF model doesn't need to change when you add an index, you're done.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.