2

I'm using EF 6 with MVC 5. I have a class defined as follows:

public class ConEdSignup
{
    [Key, Column(Order = 0)]
    public virtual ApplicationUser Attendee { get; set; }
    [Key, Column(Order = 1)]
    public virtual ConEdSession ConEdSession { get; set; }
    [Required]
    public DateTime SignupTime { get; set; }
    [Required]
    public bool Attended { get; set; }
}

This is basically a link table for a many-to-many relationship where I have additional properties about that relationship. When I try to create a migration for this, it gives me the error "Models.ConEdSignup: : EntityType 'ConEdSignup' has no key defined. Define the key for this EntityType."

I clearly defined the keys for it, but it doesn't like it. How can I use these navigation properties as the primary keys?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Jordan
  • 3,998
  • 9
  • 45
  • 81

2 Answers2

2

I believe this is because you have chosen the complex entities as they keys. You need to also define the primitives which act as the foreign keys for those entities.

TheNorthWes
  • 2,661
  • 19
  • 35
  • Yes, it does work if I do it that way. I was just hoping to not have to define them, as I don't want the primitives showing up as properties of the class. – Jordan Jun 22 '14 at 03:44
  • As far as I know you have to. That is how the EF works, and does some of its magic like how it facilitates lazy loading. – TheNorthWes Jun 22 '14 at 05:55
0

You should read this article on MSDN :-

http://msdn.microsoft.com/en-gb/data/jj679962.aspx

It explains the conventions that Entity Framework adheres too. It also explains how to create you own custom conventions.

You need to setup your classes in a specific way so that EF recognises what your trying to achieve.

Derek
  • 8,300
  • 12
  • 56
  • 88