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 string EventName { get; set; }
}
and
public class Dog
{
public int DogId { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
An event must be able to contain a collection of dogs.
Every dog must be able to be part in any event.
I’m guessing this is what they call a many-to-many relationship, but I do not now how to set it up with keys etc. I hope I’m clear enough with what I’m hoping to achieve.
I asked a similar question yesterday but I was then not clear over what I needed:
Have a list of objects as a foreign key.
Thanks!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
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.
Method 3
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).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0