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

后端 未结 2 2041
予麋鹿
予麋鹿 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<Event> Events { get; set; }
        ...
    }
    
    0 讨论(0)
  • 2021-02-14 11:10

    With Code First Approach, I would always recommend to use fluent API rather than using DataAnnotations, Which uses some conversions automatically.

    This way, you'll know what exact configuration you've made.

    If I were you, here is what i would use :

    public class EventMap : EntityTypeConfiguration<Event>
    {
        public EventMap()
        {
            this.HasRequired(m => m.CreatedBy) // envent must have a creator
                .WithMany() // a user can have 0,1 or more events created by him
                .HasForeignKey(m => m.CreatedById) // specify property to be used as FK
                .WillCascadeOnDelete(true); // delete all events created by user if that specific user is deleted
    
            this.HasMany(m=>m.Users) // an event can have 0,1 or more participants
                .WithMany(m=>m.Events) // a user can be a participant in 0,1 or more events                
                .Map(m => m.MapLeftKey("EventId").MapRightKey("UserId")); // this will generate intermediate table to hold participant information - dbo.EventUser with EventId & UserId
                // Cascade Delete is always true for Many to Many mapping. however, it doesn't delete entry in other table, it deletes entry in Joined Table only.
        }
    }
    
    0 讨论(0)
提交回复
热议问题