1

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!

3
  • Have you checked your database to see how its modeled? it look like you need to read about M to N relatioship, and get other table in order to have this working. Commented Apr 18, 2016 at 14:27
  • It's many to many Relationship. I think in Location class you must have a collection of Departement? Commented Apr 18, 2016 at 14:30
  • Check your design, as you did explain, location must have a collection of department and not the departement has a collection of location. Commented Apr 18, 2016 at 14:35

2 Answers 2

1

You need to let EF know that you have a many-to-many relation. Currently EF sees a one-to-many.

You can either add a ICollection<Department> to Location or configure it Fluently.

Documentation: Configure Many-to-Many relationship:

Sign up to request clarification or add additional context in comments.

Comments

1

The relation between your Location and Department class is many to many. meaning a Location can be related to multiple Departments and a Department can be related to multiple Locations.
Define a new property for your Location class:

public Location()
{
    this.Departments = new HashSet<Department>();
}

public virtual ICollection<Department> Departments { get; set; }

Then in your context, use fluent mapping to define the relationship appropriately:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Department>()
            .HasMany<Location>(s => s.Locations)
            .WithMany(c => c.Departments)
            .Map(cs =>
                    {
                        cs.MapLeftKey("DepartmentId");
                        cs.MapRightKey("LocationId");
                        cs.ToTable("DepartmentsLocations");
                    });

}

This will create DepartmentsLocations table in your database with two columns: DepartmentId and LocationId which will handle many-to-many relation between Departments and Locations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.