I'm trying to make a database where one of the tables has a one to one relationship and a many to many relationship ... The database created seems to be working as one to one relationship only. here's my model:
public class Trip
{
public int ID { get; set; }
public virtual ICollection<Place> PassingBy { get; set; }
public Place Origin { get; set; }
public Place Destination { get; set; }
public DateTime Time { get; set; }
public int EmptySlots { get; set; }
public virtual ICollection<Person> Attendants { get; set; }
public string AccessKey { get; set; }
}
public class Person
{
public int ID { get; set; }
public string Username { get; set; }
public virtual ICollection<Trip> Trips { get; set; }
}
public class Place
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Trip> Trips { get; set; }
}
public class GARDB : DbContext
{
public DbSet<Trip> Trips { get; set; }
public DbSet<Person> Persons { get; set; }
public DbSet<Place> Places { get; set; }
}
Now, when I do add-migrations and update database. The database has a table called "PersonTrip" which is needed for the many to many relationship. But there is no table for "PlaceTrip" . After some trial and error. I found out it's because of these lines:
public Place Origin { get; set; }
public Place Destination { get; set; }
They cause the relation to be one to many. Non many to many as I want to to be. Is there a way to make the Place-Trip relation like the Person-Trip one but without removing those 2 lines.
Thanks!
EDIT: The Solution
I didn't make clear what I wanted. I wanted to make the line
public virtual ICollection<Place> PassingBy { get; set; }
to be mapped to the line
public virtual ICollection<Trip> Trips { get; set; }
in the Place class. The solution I got to from the answers was this:
replace :
public virtual ICollection<Place> PassingBy { get; set; }
with:
[InverseProperty("Trips")]
public virtual ICollection<Place> PassingBy { get; set; }
and replace :
public virtual ICollection<Trip> Trips { get; set; }
in the Place class with:
[InverseProperty("PassingBy")]
public virtual ICollection<Trip> Trips { get; set; }
and add this include at the top:
using System.ComponentModel.DataAnnotations.Schema;