Entity framework many-to-many relationship

前端 未结 3 1630
清歌不尽
清歌不尽 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:05

    Yes, that is a N:N relationship. Assuming Code First, change your entities:

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

    And your OnModelCreating:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      modelBuilder.Entity<Dog>()
        .HasMany(d => d.Events)
        .WithMany(e => e.Dogs)
        .Map(m =>
        {
           m.MapLeftKey("DogId");
           m.MapRightKey("EventId");
           m.ToTable("DogEvent");
        });
    }
    

    This should then create a junction table DogEvent with just the two foreign keys, DogId and EventId

    0 讨论(0)
  • 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<Dog> Dogs { get; set; }
    
        public Event()
        {
            Dogs = new HashSet<Dog>();
        }
    
    }
    
    public class Dog
    {
        public int DogId { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    
        public ICollection<Event> Events { get; set; }
    
        public Dog()
        {
            Events = new HashSet<Dog>();
        }
    }
    

    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.

    0 讨论(0)
  • 2021-01-23 14:07

    You should just need an ICollection<T> property on each class:

    public class Event
    {
        public int EventId { get; set; }
        public string EventName { get; set; }
    
        public virtual ICollection<Dog> Dogs { get; set; }
    }
    
    public class Dog
    {
        public int DogId { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    
        public virtual ICollection<Event> 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).

    0 讨论(0)
提交回复
热议问题