I'm having a hard time with this problem... Basically i have 2 classes, Department and Location. Department has a ICollection of Location but Location doesn't have DepartmentID ( because a Location is not unique to a Department and the same location can be added to different Departments or different tables).
public class Department
{
public Department()
{
this.LocationList = new HashSet<Location>();
this.JobList = new HashSet<Job>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Code { get; set; }
public virtual ICollection<Location> LocationList { get; set; }
public virtual ICollection<Job> JobList { get; set; }
}
public class Location
{
public int id { get; set; }
public string Name { get; set; }
public string Adress { get; set; }
}
Whenever i try to create a Department and add a Location to it, Location gets a new attribute called Department_ID which (i think?) is the root of all my evils. So, if i add Location1 with ID = 1 and another Location2 with ID = 2, then both Locations will have that Department_ID = 1 (or another integer...). However, if i try to add Location1 to a newly created Department, that Department will "steal" that Location from the other Department's LocationList, i'm guessing it's because the Department_ID changes. How can I make it ? so it doesn't take Location1 away from the other Department? Any help would be appreciated. Thanks in advance!