F.Mota
F.Mota

Reputation: 11

Entity Framework: saving object to different entities

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!

Upvotes: 1

Views: 84

Answers (2)

Kamyar
Kamyar

Reputation: 18797

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.

Upvotes: 1

Chris
Chris

Reputation: 2019

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:

Upvotes: 1

Related Questions