Phillip Copley
Phillip Copley

Reputation: 4639

EF not recognizing Column attribute for code first?

I have the following class definition for a Code First entity:

public class Matches
{
    [Key]
    [Required]
    [Column(Order = 1)]
    public Guid MatchGroup { get; set; }

    [Key]
    [Required]
    [Column(Order = 2)]
    public long ProcedureId { get; set; }

    public long MatchLevelId { get; set; }

    [ForeignKey("ProcedureId")]
    public virtual Procedure Procedure { get; set; }

    [ForeignKey("MatchLevelId")]
    public virtual ProcedureMatchLevel MatchLevel { get; set; }
}

However when creating my initial migration I get the following error:

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

As you can see, I am using the [Column] attribute.

Has anyone ran into this issue before? I've tried switching which property my [ForeignKey] declaration is on, using [Key, ForeignKey("Procedure")] with the same error.


Procedures class:

[Table("ProcedureList")]
public class Procedure
{
    [Required]
    public int ProcedureId { get; set; }
    [Required]
    public string Code { get; set; }
    [Required]
    public string Description { get; set; }
}

Upvotes: 0

Views: 330

Answers (1)

bakare sulaimon
bakare sulaimon

Reputation: 11

You need to define the primary key for Procedure class as well

[Table("ProcedureList")]
public class Procedure
{
    [Key]
    [Required]
    public int ProcedureId { get; set; }
    [Required]
    public string Code { get; set; }
    [Required]
    public string Description { get; set; }
}

Upvotes: 1

Related Questions