Many-to-many relationship of entity structures

Ok, I'm trying to figure out how to set up my DB correctly.

I have two classes:

public class Event
{
    public int EventId { get; set; }
    public string EventName { get; set; }
}

      

and

public class Dog
{
    public int DogId { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

      

The event must contain a collection of dogs. Every dog ​​must be able to participate in any activity. I am guessing this is what they call a many-to-many relationship, but now I don't know how to set it up using keys etc. I hope I am clear enough on what I hope to achieve.

I asked a similar question yesterday, but then didn't understand what I needed: Have a list of objects as a foreign key .

Thank!

+1


source to share


3 answers


Yes, it's an N: N relationship. Assuming the code First change your objects:

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 yours 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");
    });
}

      

Then you should create a join table DogEvent

with two foreign keys DogId

andEventId

+3


source


You just need to use a property ICollection<T>

for 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 many-to-many relationships without explicitly defining the mapping, but if you want to customize the join table yourself, you can do so by overriding the method OnModelCreating

(as explained in other answers).

+2


source


It will still be 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 this 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 above code (and your previous question) is that a dog can have a "collection" of events it is associated with, and events can have a set of dogs associated with it.

+2


source







All Articles