问题
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?
回答1:
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.
回答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.
来源:https://stackoverflow.com/questions/24347196/navigation-properties-as-primary-keys