I have the following classes:
public class GoodsIssueProcess
{
[Key, Column(Order = 1), MaxLength(128)]
public string DeliveryNote { get; set; }
[Key, Column(Order = 2)]
public Product Product { get; set; }
}
public class Product
{
// the unique ID of the product
[Key]
public int Id { get; set; }
[Required, MaxLength(36)]
[Index("IX_ArticleNumber", 1, IsUnique = true)]
public string ArticleNumber { get; set; }
}
(abbreviated for readability)
As you can see, the GoodsIssueProcess consists of a Product and a delivery note code (basically just a number). According to the specification of the "Key" keyword I would expect to get a composite primary key for ProductId and DeliveryNote. Yet, what I get is a normal Primary Key for DeliveryNote. The Product column even gets the Nullable attribute and a foreign key relationship to the Product table (which is fine).
Do you have any ideas what I am doing wrong? I have also tried assigning the following:
[Key, Column(Order = 2), ForeignKey("Product")]
public int ProductId { get; set; }
public virtual Product Product { get; set; }
The result was the same database layout (only a single primary key, no composite key). I'm working on EF 6.0 and MSSQL Server 2012.
EDIT: Stop! - Apparently version #2 works (the one with public int ProductId). Yet, shouldn't the first version work as well?