Navigation Properties as Primary Keys

会有一股神秘感。 提交于 2019-12-11 00:54:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!