Many to Many with Fluent API

2

I'm doing a mapping with Fluent API and a you doubt how I should do the mapping very much.

For example, I have the classes below.

public partial class Territories
{
    public Territories()
    {
        this.Employees = new HashSet<Employees>();
    }

    public int TerritoryID { get; set; }
    public string TerritoryDescription { get; set; }
    public int RegionID { get; set; }

    public virtual Region Region { get; set; }
    public virtual ICollection<Employees> Employees { get; set; }
}

and

public partial class Employees
{
    public Employees()
    {
        this.Employees1 = new HashSet<Employees>();
        this.Orders = new HashSet<Orders>();
        this.Territories = new HashSet<Territories>();
    }

    public int EmployeeID { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Title { get; set; }
    public string TitleOfCourtesy { get; set; }
    public DateTime? BirthDate { get; set; }
    public DateTime? HireDate { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Region { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
    public string HomePhone { get; set; }
    public string Extension { get; set; }
    public byte[] Photo { get; set; }
    public string Notes { get; set; }
    public int? ReportsTo { get; set; }
    public string PhotoPath { get; set; }

    public virtual ICollection<Employees> Employees1 { get; set; }
    public virtual Employees Employees2 { get; set; }
    public virtual ICollection<Orders> Orders { get; set; }
    public virtual ICollection<Territories> Territories { get; set; }
}

In my class EmployeesMap I created the mapping.

HasMany(p => p.Territories)
.WithMany(p => p.Employees)
.Map(m =>
{
  m.ToTable("EmployeeTerritories");
  m.MapLeftKey("EmployeeID");
  m.MapRightKey("TerritoryID");
});

My question here is in my class TerritoriesMap should I make the relationship again or not? that is, to do.

HasMany(p => p.Employees)
.WithMany(p => p.Territories)
.Map(m =>
{
  m.ToTable("EmployeeTerritories");
  m.MapLeftKey("EmployeeID");
  m.MapRightKey("TerritoryID");
});
    
asked by anonymous 15.08.2017 / 15:47

2 answers

2

You decide, you do not need to do this to work. Mapping on one side only makes everything work as it should.

Remember that having navigation properties on both sides of the relationship causes a circular dependency and you'll have to deal with this in case of data serialization.

    
15.08.2017 / 15:58
2

As we can see in the example >, you only need to configure one side.

Example:

public class Student
{
    public Student() 
    {
        this.Courses = new HashSet<Course>();
    }

    public int StudentId { get; set; }
    [Required]
    public string StudentName { get; set; }

    public virtual ICollection<Course> Courses { get; set; }
}

public class Course
{
    public Course()
    {
        this.Students = new HashSet<Student>();
    }

    public int CourseId { get; set; }
    public string CourseName { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

public class SchoolDBContext : DBContext
{
    public SchoolDBContext() : base("SchoolDB-DataAnnotations")
    {
    }

    public DbSet<Student> Students { get; set; }

    public DbSet<Course> Courses { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    modelBuilder.Entity<Student>()
                .HasMany<Course>(s => s.Courses)
                .WithMany(c => c.Students)
                .Map(cs =>
                        {
                            cs.MapLeftKey("StudentRefId");
                            cs.MapRightKey("CourseRefId");
                            cs.ToTable("StudentCourse");
                        });

}

Your bank:

    
15.08.2017 / 16:10