Entity framework many-to-many relationship

前端 未结 3 1633
清歌不尽
清歌不尽 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条回答
  •  滥情空心
    2021-01-23 14:07

    It would still be quite similar to your question yesterday. See http://www.codeproject.com/Articles/234606/Creating-a-Many-To-Many-Mapping-Using-Code-First for a good blog post on the subject.

    public class Event
    {
        public int EventId { get; set; }
        public string EventName { get; set; }
    
        public ICollection Dogs { get; set; }
    
        public Event()
        {
            Dogs = new HashSet();
        }
    
    }
    
    public class Dog
    {
        public int DogId { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    
        public ICollection Events { get; set; }
    
        public Dog()
        {
            Events = new HashSet();
        }
    }
    

    The idea behind the code above (and from your previous question) is A dog can have a "collection" of events it is associated with, and events can have a collection of dogs that are associated to it.

提交回复
热议问题