Multiple primary keys with asp.net mvc 3

I am using asp.net mvc 3 and I have a problem with an entity that contains 2 primary keys when I try to insert data into a 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 paste i got this error:

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

Help me please, please!

+43
sql-server-2005 asp.net-mvc-3
May 11 '11 at 10:10
source share
1 answer

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

In 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; } } 
+70
May 11 '11 at 10:24
source share



All Articles