none
Error: Unable to determine composite primary key ordering for type RRS feed

  • Question

  • I have a database table with composite primary key. 

    I have a .net application with Code First design. I am trying to query data from this table but I get this error.

    I have tried using Column Order attribute and also HasKey in OnModelCreating methods but I still get the same error. Can anyone help me here.

    Thanks


    • Edited by Laxmikanth D Wednesday, May 8, 2019 11:45 PM
    • Moved by CoolDadTx Thursday, May 9, 2019 1:58 PM EF related
    Wednesday, May 8, 2019 11:27 PM

Answers

  • You need to mark the properties that make up the composite primary key with the key attribute

    
        [Key]
        public string PartOfPrimaryKey { get; set; }
    
        [Key]
        public int OtherPartOfPrimaryKey { get; set; }
    

    • Marked as answer by Laxmikanth D Thursday, May 9, 2019 5:09 PM
    Thursday, May 9, 2019 12:10 AM
  • Hello,

    Perhaps (from the following post)

    [Key, Column(Order = n)]

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


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Laxmikanth D Thursday, May 9, 2019 5:09 PM
    Thursday, May 9, 2019 12:29 AM

All replies

  • You need to mark the properties that make up the composite primary key with the key attribute

    
        [Key]
        public string PartOfPrimaryKey { get; set; }
    
        [Key]
        public int OtherPartOfPrimaryKey { get; set; }
    

    • Marked as answer by Laxmikanth D Thursday, May 9, 2019 5:09 PM
    Thursday, May 9, 2019 12:10 AM
  • Thanks for reply. I tried that and tried again now but issue is still there.
    Thursday, May 9, 2019 12:20 AM
  • Hello,

    Perhaps (from the following post)

    [Key, Column(Order = n)]

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


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Laxmikanth D Thursday, May 9, 2019 5:09 PM
    Thursday, May 9, 2019 12:29 AM
  • Thanks for reply. I tried with the above change with and without HasKey in OnModelCreating methos but still see this error.
    Thursday, May 9, 2019 12:32 AM
  • What edition of Entity Framework are you using?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, May 9, 2019 1:06 AM
  • Have you tried

     public class Login
    {
        public int confid { get; set; }
        [DataMember(Order = 0)]
        public int emailID  { get; set; }
        [DataMember(Order = 1)]
        public int regNo  { get; set; }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, May 9, 2019 1:10 AM