Entity Framework Code-First - Define the Non-clustered key for this EntityType
Asked Answered
M

1

0

I Have defined an entity class with a key, but I don,t want that key be cluster because I have another cluster index in my entity, but I received an error when I add an Index attribute to my model,

Can anybody tell me that how can I define a Non-Clustered Key????

Mariomariology answered 24/7, 2014 at 3:40 Comment(2)
welcome to stackoverflow, please spare a minute to read the help center, you will not get a good feedback if not providing enough information about your problem (the error message, the code, the environment, etc)..Veliavelick
Possible duplicate of create non clustered index on primary key entity framework 6.0Sandy
M
2

AFAIK it is still not possible to create non-clustered primary key fields as part of your code first configuration.

However, if you are using migrations or are in a position where you are able to, see the following Entity Framework work item that goes through a solution using code first migrations (as well as the thinking behind no direct code first support):

http://entityframework.codeplex.com/workitem/614

This related question has some information as well: EF CodeFirst create non-clustered primary key index. It is for EF 4.1 but still relevant to EF 6.

Marsipobranch answered 25/7, 2014 at 8:9 Comment(5)
the link you gave specified that it is possible with additional migration and specifying the clustered to false, but yeah u're right, no direct code first..Veliavelick
Yep - the Entity Framework work item link is pretty good. Interestingly it links off to entityframework.codeplex.com/… which are minutes from Entity Framework design meetings. I wasn't aware these were made publicly available but will be reading through them from now on. May be of interest to you (incase you didn't know about it either?) as I often see your name around Entity Framework questions.Marsipobranch
yup didn't know too, I think they can fix the clustered thing quickly, but keep it on hold because it will impact another commercial product..Veliavelick
@YuliamChandra thanx for your hints, it was useful, but would you tell me that where can i find code first migrations on the other word: where I must write statements described in the link?Mariomariology
I wouldn't recommend moving to code first migrations just to support non-clustered primary keys. Its a reasonably complex subject with a few lumpy bits on the learning curve along the way. By the sound of your question you aren't using Drop/Create database initialisers so I would just manually edit the table schema and recreate the pk as non-clustered.Marsipobranch

© 2022 - 2024 — McMap. All rights reserved.