1

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; }
}
Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

1 Answers1

0

I don't know how your code compiled at all. Try this models:

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

public class Sup
{
    [Key]
    public int Id { get; set; }
    public int Sub1Id { get; set; }
    public int Sub2Id { get; set; }
    public virtual Sub1 Sub1 { get; set; }
    public virtual Sub2 Sub2 { get; set; }
}
Slava Utesinov
  • 13,410
  • 2
  • 19
  • 26
  • You're not telling EF that those are foreign keys. How's it going to know that when we migrate in the update – Konrad Viltersten Jan 25 '16 at 07:15
  • Field Sub1 is foreign key because has type Sub1. And Sub1Id field contains this foreign key value due to **EF syntax convention**: Sub1Id = "Sub1" + "ID". – Slava Utesinov Jan 25 '16 at 07:21
  • Ah, so it's working with both ID and Id? I thought it was only one if them and, never remembering which is which, I always use it explicitly. But you say that both alternatives will create changes in my *Up()*? Good to know. – Konrad Viltersten Jan 25 '16 at 07:25