Entity Framework Code First: One-to-Many and Many-to-Many relationships to same table

后端 未结 2 2042
予麋鹿
予麋鹿 2021-02-14 10:42

I have a User model and a Event model in my project. The Event has a creator(User) and has participant(Users) so Event has a one-to-many relationship with User and also a many-t

2条回答
  •  悲&欢浪女
    2021-02-14 10:49

    EF doesn't know where User.Events has to be mapped to. It could be Event.CreatedBy or it could be Event.Users. Both would result in a valid model. You must give EF a little hint what you want by applying the [InverseProperty] attribute:

    public class User
    {
        ...
        [InverseProperty("Users")]
        public virtual ICollection Events { get; set; }
        ...
    }
    

提交回复
热议问题