Share via


Column '' in table '' is of a type that is invalid for use as a key column in an index

I decided to change my blogging style a little bit. Instead of writing lengthy blog posts in a more systematic way I want to write things about all the different kinds of issue I got when building stuff and learning new things. I am very experienced in some areas but new on many more areas so the issues I got might be very basic and simple. I think that it is useful to just record it for myself and also for other people since they might get into the same situation.

I just started using entity framework 6.1.1 and tried to define an index on a column called ZipCode as below but I got the below error when doing 'Update-Database' by using code first migrations.

 public string ZipCode { get; set; }

Column 'ZipCode' in table 'dbo.ServiceProviders' is of a type that is invalid for use as a key column in an index.

It turned out that it is a simple issue. The max length for an index column is 900 bytes. The fix is as simple as below.

  [MaxLength(20)]
 public string ZipCode { get; set; }
 

Comments

  • Anonymous
    August 18, 2014
    Thanks, this saved me from a big problem

  • Anonymous
    December 09, 2014
    Thanks ... saved me time :)

  • Anonymous
    January 13, 2015
    Thank. You. I was chasing this down as  a Data Type issue, rather than just a field length issue.  Saved me a lot of time.

  • Anonymous
    March 12, 2015
    Thank you for the blog. Saved me heaps of time.

  • Anonymous
    July 23, 2015
    Was exactly that the problem. You nail it Thx

  • Anonymous
    October 19, 2015
    Was at the top of my Bing search, where it belongs. Thanks.

  • Anonymous
    November 18, 2015
    Can this be corrected from the edmx (design first), or is source code the only way (code first)?

  • Anonymous
    November 18, 2015
    I didn't try design first approach since I prefer code first but I think that there should be an equivalent way.

  • Anonymous
    January 22, 2016
    Kudos!! Solved my problem. Thanks. Clearly a bad message on the part of Entity Framework ...