Fixing the error: “Column XX in table dbo.YY is of a type that is invalid for use as a key column in an index.”

Reading Time: 2 minutes. This post was most recently updated on October 9th, 2018.While using Entity Framework and code-first migrations, Entity Framework (EF) creates the indexes for you – but what if you need to create a custom one, explicitly based on certain field / column? Then you’ll have to tell EF which one to use as a key column. Usually, it’s easy – you just add the following annotation to the columns you’ll be using: [Index(“OfficialIds”, 1)] public int AreaId { get; set; } [Index(“OfficialIds”, 2)] public string EstateId { get; set; } (example stripped of extra code and other columns for clarity) And after adding the migration (Add-Migration… commandlet) you get something like this: CreateIndex(“dbo.Areas”, new[] { “AreaId”, “EstateId” }, name: “OfficialIds”); Okay, nice. […]