Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Primary Key with asp .net mvc 3

I'm using asp .net mvc 3, and I have a problem with an entity that contains 2 primary key, when I try to insert data in the table.

 public class LineItem     {         [Key]         public int OrderId { get; set;}         [Key]         public int LineNum  { get; set;}         public string ItemId { get; set;}         public int Quantity { get; set;}         public decimal UnitPrice { get; set; }      } 

when I try to insert I got this error :

Unable to determine composite primary key ordering for type 'ApplicationMVC3.Models.LineItem'. Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys.

May someone help me please !!

like image 821
Sarroura Avatar asked May 11 '11 10:05

Sarroura


1 Answers

Assuming this is actually a composite key, since you can't have 2 primary keys... The error message tells you exactly what to do, namely add an order. You can do this by adding [Column(Order = 0)] and [Column(Order = 1)] to your key columns.

For your example:

public class LineItem     {         [Key][Column(Order = 0)]         public int OrderId { get; set;}         [Key][Column(Order = 1)]         public int LineNum  { get; set;}         public string ItemId { get; set;}         public int Quantity { get; set;}         public decimal UnitPrice { get; set; }      } 
like image 116
verdesmarald Avatar answered Sep 21 '22 17:09

verdesmarald