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 (9)

  1. Saso says:

    Thanks, this saved me from a big problem

  2. Arun says:

    Thanks … saved me time 🙂

  3. CC says:

    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.

  4. DannyT says:

    Thank you for the blog. Saved me heaps of time.

  5. BernardoBL says:

    Was exactly that the problem.

    You nail it Thx

  6. Joe Mayo says:

    Was at the top of my Bing search, where it belongs. Thanks.

  7. John Maurer says:

    Can this be corrected from the edmx (design first), or is source code the only way (code first)?

  8. Paul Lou says:

    I didn't try design first approach since I prefer code first but I think that there should be an equivalent way.

  9. silverfox1948 says:

    Kudos!! Solved my problem. Thanks. Clearly a bad message on the part of Entity Framework …