Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys

Viewed 37290

I'm trying to use composite primary key on 2 objects with parent-child relationship. Whenever I try to create a new migration, I get an error:

Unable to determine composite primary key ordering for type 'Models.UserProjectRole'. Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys.

As per error suggests, I do add annotation Column (Order = X) but the error still there and does not go away, unless I leave only one field with Key annotation. Here is my object that it trips off:

public class UserProjectRole
{
    [Key, Column(Order = 0),DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid UserProjectRoleID { get; set; }

    [Key, Column (Order = 1)]
    public Guid ProjectID { get; set; }

    [ForeignKey("ProjectID")]
    public Project Project { get; set; }

    public Guid AppUserGuid { get; set; }

    // followed by a number of unrelated String fields.
 }

Here is Project class:

public class Project: Base
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid ProjectID { get; set; }

    public virtual ICollection<UserProjectRole> UserRoles { get; set; }

    // followed by a number of unrelated String fields.

}

Here is part of my DBContext:

public class SiteContext : DbContext
{

    public DbSet<Project> Projects { get; set; }

    public DbSet<UserProjectRole> UserProjectRoles { get; set; }
}

I'm in VisualStudio 2012 with EF 4.3.1

I have been banging my head against this for a while now and all the forum and SO answers suggest to add Column Order annotation that I already have. Am I missing something obvious???

Thank you for reading this far -)

4 Answers
Related