Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to determine composite primary key ordering for type

{"Unable to determine composite primary key ordering for type 'Conference_Project.Models.Login'. Use the ColumnAttribute (see http://go.microsoft.com/fwlink/?LinkId=386388) or the HasKey method (see http://go.microsoft.com/fwlink/?LinkId=386387) to specify an order for composite primary keys."}

 [Table("ConferenceLogin")]
public class Login
{

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long confid { get; set; }
    [Key]
    public string emailID { get; set; }       
    [Key]
    public string registration { get; set; }
    [Key]
    public long regNo { get; set; }        
}

enter image description here

Version Used : EntityFramework.6.1.3  And MVC5

I want unique value for this (emailID , registration , regNo ) for that set all as primary key then EntityFramework showed error

how to use multiple primary key with EntityFramework?

like image 273
User Avatar asked Mar 16 '17 03:03

User


1 Answers

Although I'm not sure as to the underlying logic why this error occurs, I did have the same issue with a project I was working on for class. What worked for me was to add a Column(order) decorator to the Key tag turning

[Key] 

into

[Key, Column(Order = n)]

where n is the 0-based index of the key in question.

In this light, your class should look like the following:

[Table("ConferenceLogin")]
public class Login
{
    [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long confid { get; set; }
    [Key, Column(Order = 1)]
    public string emailID { get; set; }       
    [Key, Column(Order = 2)]
    public string registration { get; set; }
    [Key, Column(Order = 3)]
    public long regNo { get; set; }        
}

Hope this works for you as well as it did for me!

like image 129
Patrick R. Conner Avatar answered Sep 21 '22 06:09

Patrick R. Conner