I have two classes, each with a single primary key (verified in the DB too - there's only one key and only one column is PK).
public class Sub1
{
[Key]public int Id {get; set;}
[Required]public int Value {get; set;}
}
public class Sub2
{
[Key]public int Id {get; set;}
[Required]public int Value {get; set;}
}
Then I add a third class, that uses the Id columns in the two above as foreign keys.
public class Sup
{
[Key]public int Id { get; set; }
[ForeignKey(Sub1)]public int Sub1Id { get; set; }
[ForeignKey(Sub2)]public int Sub2Id { get; set; }
public virtual Sub1 { get; set; }
public virtual Sub2 { get; set; }
}
When I try to run Add-Migration, I get the error below. There are no composite primary keys nor do I have multiple primary keys anywhere. When I add the Column attribute, it works but I feel that it shouldn't be necessary (hence, I suspect that I'm doing something wrong).
Unable to determine a composite foreign key ordering for foreign key on type Beep.Bapp.Thing. When using the ForeignKey data annotation on composite foreign key properties ensure order is specified by using the Column data annotation or the fluent API.
In this answer, there's the class Category and here I get htat we need to add the columns ordering information.
public class Category
{
[Key, Column(Order = 0)]
public int CategoryId2 { get; set; }
[Key, Column(Order = 1)]
public int CategoryId3 { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
public class Product
{
[Key]
public int ProductId { get; set; }
public string Name { get; set; }
[ForeignKey("Category"), Column(Order = 0)]
public int CategoryId2 { get; set; }
[ForeignKey("Category"), Column(Order = 1)]
public int CategoryId3 { get; set; }
public virtual Category Category { get; set; }
}