13
votes

{"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?

2
how to use multiple primary key with EntityFramework? Click and read the links in the exception message. - Ivan Stoev
Have you want to create n:n (many-to-many) relationship between tables with Code First (show all tables related with it)? Usually HasKey before CreateModel() used like this: builder.Entity<ForeignTablePropertyName>().HasKey(p => new { p.emailID, p.registration, p.regNo });. - Tetsuya Yamamoto
solution already available in my error link its only need to set [Column(Order = 1)] order number - User

2 Answers

21
votes

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!

0
votes

To set that registration column as primary key, you have to set the data type nvarchar(50),. nvarchar(128) will not allow you to make it primary key