0

I've been searchin around a lot for a way to create, or configure a specific index on a table from EF code first, but really can't find a thing. mapping fluent Api doesn't seem to expose any means to do this. What am I missing?

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Amanda Tarafa Mas
  • 1,043
  • 8
  • 13
  • 1
    You're not missing anything. Code first is missing that. See http://stackoverflow.com/questions/8262590/entity-framework-code-first-fluent-api-adding-indexes-to-columns – podiluska Aug 08 '12 at 08:58
  • Thank you. I was almost sure it wasn't possible with fluent api or dataannotations, but confirmation is always nice. I'll try some workarounds and if I find something interesting, I'l post it here. Thanks again – Amanda Tarafa Mas Aug 08 '12 at 09:07

1 Answers1

1

You can add index creation either to custom database intitializer as mentioned in linked answer or you can use code based migrations. Migrations provide support for index creation.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Yes, but ir feels wrong having to add a migration for something that is not really a change in your database but part of the initial definition. Creating a Index, or constraint should be part of the mapping process as well as the migration process. As a workaround is a nice one though. – Amanda Tarafa Mas Aug 08 '12 at 09:20
  • You can scaffold your initial model definition as initial code based migration and add index creation. Anyway there is currently no better way. – Ladislav Mrnka Aug 08 '12 at 09:26
  • The reason it feels wrong is that you understand databases. I would allege that code first is for people who don't. – podiluska Aug 08 '12 at 09:58
  • If you don't understand database you most probably don't know about indices. I think there are some indices created automatically when using migrations. – Ladislav Mrnka Aug 08 '12 at 10:01
  • With mapping, indexes are indeed created, somo of them redundant even. I wouldn't say code first is for people who don't understand DB, I like it, and I love the fact that it can be used out of the box for some simple yet usual scenarios. I miss some advanced config features for some advanced scenarios. This one posted here is one of them, and also [this one](http://stackoverflow.com/questions/11799328/entityframework-4-3-1-code-first-navigation-property-mapping-and-visibility). – Amanda Tarafa Mas Aug 08 '12 at 10:31