Enity Framework “column in table is of a type that is invalid for use as a key column in an index”

By | June 9, 2014

Advertisement from Google

After adding Index attribute with IsUnique set to true for string value in my code first model

 [Index(IsUnique = true)]
 public string Email { get; set; }

, I’ve tried to update my database, but got following error:

“column in table is of a type that is invalid for use as a key column in an index”.


Advertisement from Google

I’ve got this error because unique constraint can’t be over 8000 bytes per row and will only use the first 900 bytes even then so the safest maximum size for my field would be 450 characters.

To add this limitation in code first model just add following attribute to your property:

[MaxLength(450)]

Now you can create migration and perform Update-Database command.

One thought on “Enity Framework “column in table is of a type that is invalid for use as a key column in an index”

Leave a Reply

Your email address will not be published. Required fields are marked *