0

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; }
}
Phillip Copley
  • 4,238
  • 4
  • 21
  • 38
  • What does your DbContext class look like and your Procedure class look like? I think that the problem may be in the Procedure class not the Matches class. – Mike Wodarczyk Mar 11 '19 at 15:12
  • It does work if I use Fluent API (`modelBuilder.Entity().HasKey(x => new { x.MatchGroup, x.ProcedureId });`, however I'm not using Fluent API for anything else so I'd like to know what I'm doing wrong with the Data Attributes to maintain some consistency. – Phillip Copley Mar 11 '19 at 15:13
  • Thanks @MikeWodarczyk, I will update with my procedure class momentarily. – Phillip Copley Mar 11 '19 at 15:14
  • 1
    The column order index begins at 0, are you sure your orders are 1 & 2 and not 0 & 1 – Daniel Mar 11 '19 at 15:15
  • 1
    In [this related or duplicated](https://stackoverflow.com/questions/19792295/mapping-composite-keys-using-ef-code-first) I see the Order is zero based index. – Cleptus Mar 11 '19 at 15:16
  • @Daniel It is zero index and you are correct I should use 0/1 and not 1/2; still get the error with these values, however. – Phillip Copley Mar 11 '19 at 15:19
  • Have you tried what @Daniel said? – Gabriel Costin Mar 11 '19 at 15:19
  • 2
    @PhillipCopley look at procedure class, why you have no primary key? – Gabriel Costin Mar 11 '19 at 15:20
  • Possible duplicate of [Mapping composite keys using EF code first](https://stackoverflow.com/questions/19792295/mapping-composite-keys-using-ef-code-first) – David Browne - Microsoft Mar 11 '19 at 17:04
  • @DavidBrowne-Microsoft No, the `Order` of the composite key is present, your link is not duplicated but related. The problem is on the `Procedure` class, it should have a `[Key]` attribute but is missing and because of that the `Matches` class has a `[Foreign]` that "points to nowhere". – Cleptus Mar 12 '19 at 09:43

1 Answers1

1

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; }
}
Cleptus
  • 3,446
  • 4
  • 28
  • 34