简体   繁体   中英

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; }
}

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM