Entity framework many-to-many relationship

前端 未结 3 1634
清歌不尽
清歌不尽 2021-01-23 13:39

Ok, I\'m trying to figure out how to setup my DB properly.

I have two classes:

public class Event
{
    public int EventId { get; set; }
    public stri         


        
3条回答
  •  旧时难觅i
    2021-01-23 14:07

    You should just need an ICollection property on each class:

    public class Event
    {
        public int EventId { get; set; }
        public string EventName { get; set; }
    
        public virtual ICollection Dogs { get; set; }
    }
    
    public class Dog
    {
        public int DogId { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    
        public virtual ICollection Events { get; set; }
    }
    

    EF should be able to handle a many-to-many relationship without you explicitly defining the mapping, but if you want to set up the junction table yourself, you can do that by overriding the OnModelCreating method (as explained in other answers).

提交回复
热议问题